174914bca936c44e34c994282a82cd968b9c716b
[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
41 (defcustom gnus-kill-summary-on-exit t
42   "*If non-nil, kill the summary buffer when you exit from it.
43 If nil, the summary will become a \"*Dead Summary*\" buffer, and
44 it will be killed sometime later."
45   :group 'gnus-summary-exit
46   :type 'boolean)
47
48 (defcustom gnus-fetch-old-headers nil
49   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
50 If an unread article in the group refers to an older, already read (or
51 just marked as read) article, the old article will not normally be
52 displayed in the Summary buffer.  If this variable is non-nil, Gnus
53 will attempt to grab the headers to the old articles, and thereby
54 build complete threads.  If it has the value `some', only enough
55 headers to connect otherwise loose threads will be displayed.  This
56 variable can also be a number.  In that case, no more than that number
57 of old headers will be fetched.  If it has the value `invisible', all
58 old headers will be fetched, but none will be displayed.
59
60 The server has to support NOV for any of this to work."
61   :group 'gnus-thread
62   :type '(choice (const :tag "off" nil)
63                  (const some)
64                  number
65                  (sexp :menu-tag "other" t)))
66
67 (defcustom gnus-refer-thread-limit 200
68   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
69 If t, fetch all the available old headers."
70   :group 'gnus-thread
71   :type '(choice number
72                  (sexp :menu-tag "other" t)))
73
74 (defcustom gnus-summary-make-false-root 'adopt
75   "*nil means that Gnus won't gather loose threads.
76 If the root of a thread has expired or been read in a previous
77 session, the information necessary to build a complete thread has been
78 lost.  Instead of having many small sub-threads from this original thread
79 scattered all over the summary buffer, Gnus can gather them.
80
81 If non-nil, Gnus will try to gather all loose sub-threads from an
82 original thread into one large thread.
83
84 If this variable is non-nil, it should be one of `none', `adopt',
85 `dummy' or `empty'.
86
87 If this variable is `none', Gnus will not make a false root, but just
88 present the sub-threads after another.
89 If this variable is `dummy', Gnus will create a dummy root that will
90 have all the sub-threads as children.
91 If this variable is `adopt', Gnus will make one of the \"children\"
92 the parent and mark all the step-children as such.
93 If this variable is `empty', the \"children\" are printed with empty
94 subject fields.  (Or rather, they will be printed with a string
95 given by the `gnus-summary-same-subject' variable.)"
96   :group 'gnus-thread
97   :type '(choice (const :tag "off" nil)
98                  (const none)
99                  (const dummy)
100                  (const adopt)
101                  (const empty)))
102
103 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
104   "*A regexp to match subjects to be excluded from loose thread gathering.
105 As loose thread gathering is done on subjects only, that means that
106 there can be many false gatherings performed.  By rooting out certain
107 common subjects, gathering might become saner."
108   :group 'gnus-thread
109   :type 'regexp)
110
111 (defcustom gnus-summary-gather-subject-limit nil
112   "*Maximum length of subject comparisons when gathering loose threads.
113 Use nil to compare full subjects.  Setting this variable to a low
114 number will help gather threads that have been corrupted by
115 newsreaders chopping off subject lines, but it might also mean that
116 unrelated articles that have subject that happen to begin with the
117 same few characters will be incorrectly gathered.
118
119 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
120 comparing subjects."
121   :group 'gnus-thread
122   :type '(choice (const :tag "off" nil)
123                  (const fuzzy)
124                  (sexp :menu-tag "on" t)))
125
126 (defcustom gnus-simplify-subject-functions nil
127   "List of functions taking a string argument that simplify subjects.
128 The functions are applied recursively.
129
130 Useful functions to put in this list include: `gnus-simplify-subject-re',
131 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
132   :group 'gnus-thread
133   :type '(repeat function))
134
135 (defcustom gnus-simplify-ignored-prefixes nil
136   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
137   :group 'gnus-thread
138   :type '(choice (const :tag "off" nil)
139                  regexp))
140
141 (defcustom gnus-build-sparse-threads nil
142   "*If non-nil, fill in the gaps in threads.
143 If `some', only fill in the gaps that are needed to tie loose threads
144 together.  If `more', fill in all leaf nodes that Gnus can find.  If
145 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
146   :group 'gnus-thread
147   :type '(choice (const :tag "off" nil)
148                  (const some)
149                  (const more)
150                  (sexp :menu-tag "all" t)))
151
152 (defcustom gnus-summary-thread-gathering-function
153   'gnus-gather-threads-by-subject
154   "*Function used for gathering loose threads.
155 There are two pre-defined functions: `gnus-gather-threads-by-subject',
156 which only takes Subjects into consideration; and
157 `gnus-gather-threads-by-references', which compared the References
158 headers of the articles to find matches."
159   :group 'gnus-thread
160   :type '(radio (function-item gnus-gather-threads-by-subject)
161                 (function-item gnus-gather-threads-by-references)
162                 (function :tag "other")))
163
164 (defcustom gnus-summary-same-subject ""
165   "*String indicating that the current article has the same subject as the previous.
166 This variable will only be used if the value of
167 `gnus-summary-make-false-root' is `empty'."
168   :group 'gnus-summary-format
169   :type 'string)
170
171 (defcustom gnus-summary-goto-unread t
172   "*If t, many commands will go to the next unread article.
173 This applies to marking commands as well as other commands that
174 \"naturally\" select the next article, like, for instance, `SPC' at
175 the end of an article.
176
177 If nil, the marking commands do NOT go to the next unread article
178 (they go to the next article instead).  If `never', commands that
179 usually go to the next unread article, will go to the next article,
180 whether it is read or not."
181   :group 'gnus-summary-marks
182   :link '(custom-manual "(gnus)Setting Marks")
183   :type '(choice (const :tag "off" nil)
184                  (const never)
185                  (sexp :menu-tag "on" t)))
186
187 (defcustom gnus-summary-default-score 0
188   "*Default article score level.
189 All scores generated by the score files will be added to this score.
190 If this variable is nil, scoring will be disabled."
191   :group 'gnus-score-default
192   :type '(choice (const :tag "disable")
193                  integer))
194
195 (defcustom gnus-summary-zcore-fuzz 0
196   "*Fuzziness factor for the zcore in the summary buffer.
197 Articles with scores closer than this to `gnus-summary-default-score'
198 will not be marked."
199   :group 'gnus-summary-format
200   :type 'integer)
201
202 (defcustom gnus-simplify-subject-fuzzy-regexp nil
203   "*Strings to be removed when doing fuzzy matches.
204 This can either be a regular expression or list of regular expressions
205 that will be removed from subject strings if fuzzy subject
206 simplification is selected."
207   :group 'gnus-thread
208   :type '(repeat regexp))
209
210 (defcustom gnus-show-threads t
211   "*If non-nil, display threads in summary mode."
212   :group 'gnus-thread
213   :type 'boolean)
214
215 (defcustom gnus-thread-hide-subtree nil
216   "*If non-nil, hide all threads initially.
217 If threads are hidden, you have to run the command
218 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
219 to expose hidden threads."
220   :group 'gnus-thread
221   :type 'boolean)
222
223 (defcustom gnus-thread-hide-killed t
224   "*If non-nil, hide killed threads automatically."
225   :group 'gnus-thread
226   :type 'boolean)
227
228 (defcustom gnus-thread-ignore-subject t
229   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
230 If nil, articles that have different subjects from their parents will
231 start separate threads."
232   :group 'gnus-thread
233   :type 'boolean)
234
235 (defcustom gnus-thread-operation-ignore-subject t
236   "*If non-nil, subjects will be ignored when doing thread commands.
237 This affects commands like `gnus-summary-kill-thread' and
238 `gnus-summary-lower-thread'.
239
240 If this variable is nil, articles in the same thread with different
241 subjects will not be included in the operation in question.  If this
242 variable is `fuzzy', only articles that have subjects that are fuzzily
243 equal will be included."
244   :group 'gnus-thread
245   :type '(choice (const :tag "off" nil)
246                  (const fuzzy)
247                  (sexp :tag "on" t)))
248
249 (defcustom gnus-thread-indent-level 4
250   "*Number that says how much each sub-thread should be indented."
251   :group 'gnus-thread
252   :type 'integer)
253
254 (defcustom gnus-auto-extend-newsgroup t
255   "*If non-nil, extend newsgroup forward and backward when requested."
256   :group 'gnus-summary-choose
257   :type 'boolean)
258
259 (defcustom gnus-auto-select-first t
260   "*If nil, don't select the first unread article when entering a group.
261 If this variable is `best', select the highest-scored unread article
262 in the group.  If t, select the first unread article.
263
264 This variable can also be a function to place point on a likely
265 subject line.  Useful values include `gnus-summary-first-unread-subject',
266 `gnus-summary-first-unread-article' and
267 `gnus-summary-best-unread-article'.
268
269 If you want to prevent automatic selection of the first unread article
270 in some newsgroups, set the variable to nil in
271 `gnus-select-group-hook'."
272   :group 'gnus-group-select
273   :type '(choice (const :tag "none" nil)
274                  (const best)
275                  (sexp :menu-tag "first" t)
276                  (function-item gnus-summary-first-unread-subject)
277                  (function-item gnus-summary-first-unread-article)
278                  (function-item gnus-summary-best-unread-article)))
279
280 (defcustom gnus-auto-select-next t
281   "*If non-nil, offer to go to the next group from the end of the previous.
282 If the value is t and the next newsgroup is empty, Gnus will exit
283 summary mode and go back to group mode.  If the value is neither nil
284 nor t, Gnus will select the following unread newsgroup.  In
285 particular, if the value is the symbol `quietly', the next unread
286 newsgroup will be selected without any confirmation, and if it is
287 `almost-quietly', the next group will be selected without any
288 confirmation if you are located on the last article in the group.
289 Finally, if this variable is `slightly-quietly', the `Z n' command
290 will go to the next group without confirmation."
291   :group 'gnus-summary-maneuvering
292   :type '(choice (const :tag "off" nil)
293                  (const quietly)
294                  (const almost-quietly)
295                  (const slightly-quietly)
296                  (sexp :menu-tag "on" t)))
297
298 (defcustom gnus-auto-select-same nil
299   "*If non-nil, select the next article with the same subject.
300 If there are no more articles with the same subject, go to
301 the first unread article."
302   :group 'gnus-summary-maneuvering
303   :type 'boolean)
304
305 (defcustom gnus-summary-check-current nil
306   "*If non-nil, consider the current article when moving.
307 The \"unread\" movement commands will stay on the same line if the
308 current article is unread."
309   :group 'gnus-summary-maneuvering
310   :type 'boolean)
311
312 (defcustom gnus-auto-center-summary t
313   "*If non-nil, always center the current summary buffer.
314 In particular, if `vertical' do only vertical recentering.  If non-nil
315 and non-`vertical', do both horizontal and vertical recentering."
316   :group 'gnus-summary-maneuvering
317   :type '(choice (const :tag "none" nil)
318                  (const vertical)
319                  (integer :tag "height")
320                  (sexp :menu-tag "both" t)))
321
322 (defcustom gnus-show-all-headers nil
323   "*If non-nil, don't hide any headers."
324   :group 'gnus-article-hiding
325   :group 'gnus-article-headers
326   :type 'boolean)
327
328 (defcustom gnus-summary-ignore-duplicates nil
329   "*If non-nil, ignore articles with identical Message-ID headers."
330   :group 'gnus-summary
331   :type 'boolean)
332
333 (defcustom gnus-single-article-buffer t
334   "*If non-nil, display all articles in the same buffer.
335 If nil, each group will get its own article buffer."
336   :group 'gnus-article-various
337   :type 'boolean)
338
339 (defcustom gnus-break-pages t
340   "*If non-nil, do page breaking on articles.
341 The page delimiter is specified by the `gnus-page-delimiter'
342 variable."
343   :group 'gnus-article-various
344   :type 'boolean)
345
346 (defcustom gnus-move-split-methods nil
347   "*Variable used to suggest where articles are to be moved to.
348 It uses the same syntax as the `gnus-split-methods' variable."
349   :group 'gnus-summary-mail
350   :type '(repeat (choice (list :value (fun) function)
351                          (cons :value ("" "") regexp (repeat string))
352                          (sexp :value nil))))
353
354 (defcustom gnus-unread-mark ?  ;Whitespace
355   "*Mark used for unread articles."
356   :group 'gnus-summary-marks
357   :type 'character)
358
359 (defcustom gnus-ticked-mark ?!
360   "*Mark used for ticked articles."
361   :group 'gnus-summary-marks
362   :type 'character)
363
364 (defcustom gnus-dormant-mark ??
365   "*Mark used for dormant articles."
366   :group 'gnus-summary-marks
367   :type 'character)
368
369 (defcustom gnus-del-mark ?r
370   "*Mark used for del'd articles."
371   :group 'gnus-summary-marks
372   :type 'character)
373
374 (defcustom gnus-read-mark ?R
375   "*Mark used for read articles."
376   :group 'gnus-summary-marks
377   :type 'character)
378
379 (defcustom gnus-expirable-mark ?E
380   "*Mark used for expirable articles."
381   :group 'gnus-summary-marks
382   :type 'character)
383
384 (defcustom gnus-killed-mark ?K
385   "*Mark used for killed articles."
386   :group 'gnus-summary-marks
387   :type 'character)
388
389 (defcustom gnus-souped-mark ?F
390   "*Mark used for killed articles."
391   :group 'gnus-summary-marks
392   :type 'character)
393
394 (defcustom gnus-kill-file-mark ?X
395   "*Mark used for articles killed by kill files."
396   :group 'gnus-summary-marks
397   :type 'character)
398
399 (defcustom gnus-low-score-mark ?Y
400   "*Mark used for articles with a low score."
401   :group 'gnus-summary-marks
402   :type 'character)
403
404 (defcustom gnus-catchup-mark ?C
405   "*Mark used for articles that are caught up."
406   :group 'gnus-summary-marks
407   :type 'character)
408
409 (defcustom gnus-replied-mark ?A
410   "*Mark used for articles that have been replied to."
411   :group 'gnus-summary-marks
412   :type 'character)
413
414 (defcustom gnus-cached-mark ?*
415   "*Mark used for articles that are in the cache."
416   :group 'gnus-summary-marks
417   :type 'character)
418
419 (defcustom gnus-saved-mark ?S
420   "*Mark used for articles that have been saved to."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-ancient-mark ?O
425   "*Mark used for ancient articles."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-sparse-mark ?Q
430   "*Mark used for sparsely reffed articles."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-canceled-mark ?G
435   "*Mark used for canceled articles."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-duplicate-mark ?M
440   "*Mark used for duplicate articles."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-undownloaded-mark ?@
445   "*Mark used for articles that weren't downloaded."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-downloadable-mark ?%
450   "*Mark used for articles that are to be downloaded."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-unsendable-mark ?=
455   "*Mark used for articles that won't be sent."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-score-over-mark ?+
460   "*Score mark used for articles with high scores."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-score-below-mark ?-
465   "*Score mark used for articles with low scores."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-empty-thread-mark ?  ;Whitespace
470   "*There is no thread under the article."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-not-empty-thread-mark ?=
475   "*There is a thread under the article."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-view-pseudo-asynchronously nil
480   "*If non-nil, Gnus will view pseudo-articles asynchronously."
481   :group 'gnus-extract-view
482   :type 'boolean)
483
484 (defcustom gnus-auto-expirable-marks
485   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
486         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
487         gnus-souped-mark gnus-duplicate-mark)
488   "*The list of marks converted into expiration if a group is auto-expirable."
489   :group 'gnus-summary
490   :type '(repeat character))
491
492 (defcustom gnus-inhibit-user-auto-expire t
493   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
494   :group 'gnus-summary
495   :type 'boolean)
496
497 (defcustom gnus-view-pseudos nil
498   "*If `automatic', pseudo-articles will be viewed automatically.
499 If `not-confirm', pseudos will be viewed automatically, and the user
500 will not be asked to confirm the command."
501   :group 'gnus-extract-view
502   :type '(choice (const :tag "off" nil)
503                  (const automatic)
504                  (const not-confirm)))
505
506 (defcustom gnus-view-pseudos-separately t
507   "*If non-nil, one pseudo-article will be created for each file to be viewed.
508 If nil, all files that use the same viewing command will be given as a
509 list of parameters to that command."
510   :group 'gnus-extract-view
511   :type 'boolean)
512
513 (defcustom gnus-insert-pseudo-articles t
514   "*If non-nil, insert pseudo-articles when decoding articles."
515   :group 'gnus-extract-view
516   :type 'boolean)
517
518 (defcustom gnus-summary-dummy-line-format
519   "  %(:                          :%) %S\n"
520   "*The format specification for the dummy roots in the summary buffer.
521 It works along the same lines as a normal formatting string,
522 with some simple extensions.
523
524 %S  The subject"
525   :group 'gnus-threading
526   :type 'string)
527
528 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
529   "*The format specification for the summary mode line.
530 It works along the same lines as a normal formatting string,
531 with some simple extensions:
532
533 %G  Group name
534 %p  Unprefixed group name
535 %A  Current article number
536 %z  Current article score
537 %V  Gnus version
538 %U  Number of unread articles in the group
539 %e  Number of unselected articles in the group
540 %Z  A string with unread/unselected article counts
541 %g  Shortish group name
542 %S  Subject of the current article
543 %u  User-defined spec
544 %s  Current score file name
545 %d  Number of dormant articles
546 %r  Number of articles that have been marked as read in this session
547 %E  Number of articles expunged by the score files"
548   :group 'gnus-summary-format
549   :type 'string)
550
551 (defcustom gnus-list-identifiers nil
552   "Regexp that matches list identifiers to be removed from subject.
553 This can also be a list of regexps."
554   :group 'gnus-summary-format
555   :group 'gnus-article-hiding
556   :type '(choice (const :tag "none" nil)
557                  (regexp :value ".*")
558                  (repeat :value (".*") regexp)))
559
560 (defcustom gnus-summary-mark-below 0
561   "*Mark all articles with a score below this variable as read.
562 This variable is local to each summary buffer and usually set by the
563 score file."
564   :group 'gnus-score-default
565   :type 'integer)
566
567 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
568   "*List of functions used for sorting articles in the summary buffer.
569 This variable is only used when not using a threaded display."
570   :group 'gnus-summary-sort
571   :type '(repeat (choice (function-item gnus-article-sort-by-number)
572                          (function-item gnus-article-sort-by-author)
573                          (function-item gnus-article-sort-by-subject)
574                          (function-item gnus-article-sort-by-date)
575                          (function-item gnus-article-sort-by-score)
576                          (function :tag "other"))))
577
578 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
579   "*List of functions used for sorting threads in the summary buffer.
580 By default, threads are sorted by article number.
581
582 Each function takes two threads and return non-nil if the first thread
583 should be sorted before the other.  If you use more than one function,
584 the primary sort function should be the last.  You should probably
585 always include `gnus-thread-sort-by-number' in the list of sorting
586 functions -- preferably first.
587
588 Ready-made functions include `gnus-thread-sort-by-number',
589 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
590 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
591 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
592   :group 'gnus-summary-sort
593   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
594                          (function-item gnus-thread-sort-by-author)
595                          (function-item gnus-thread-sort-by-subject)
596                          (function-item gnus-thread-sort-by-date)
597                          (function-item gnus-thread-sort-by-score)
598                          (function-item gnus-thread-sort-by-total-score)
599                          (function :tag "other"))))
600
601 (defcustom gnus-thread-score-function '+
602   "*Function used for calculating the total score of a thread.
603
604 The function is called with the scores of the article and each
605 subthread and should then return the score of the thread.
606
607 Some functions you can use are `+', `max', or `min'."
608   :group 'gnus-summary-sort
609   :type 'function)
610
611 (defcustom gnus-summary-expunge-below nil
612   "All articles that have a score less than this variable will be expunged.
613 This variable is local to the summary buffers."
614   :group 'gnus-score-default
615   :type '(choice (const :tag "off" nil)
616                  integer))
617
618 (defcustom gnus-thread-expunge-below nil
619   "All threads that have a total score less than this variable will be expunged.
620 See `gnus-thread-score-function' for en explanation of what a
621 \"thread score\" is.
622
623 This variable is local to the summary buffers."
624   :group 'gnus-threading
625   :group 'gnus-score-default
626   :type '(choice (const :tag "off" nil)
627                  integer))
628
629 (defcustom gnus-summary-mode-hook nil
630   "*A hook for Gnus summary mode.
631 This hook is run before any variables are set in the summary buffer."
632   :group 'gnus-summary-various
633   :type 'hook)
634
635 (defcustom gnus-summary-menu-hook nil
636   "*Hook run after the creation of the summary mode menu."
637   :group 'gnus-summary-visual
638   :type 'hook)
639
640 (defcustom gnus-summary-exit-hook nil
641   "*A hook called on exit from the summary buffer.
642 It will be called with point in the group buffer."
643   :group 'gnus-summary-exit
644   :type 'hook)
645
646 (defcustom gnus-summary-prepare-hook nil
647   "*A hook called after the summary buffer has been generated.
648 If you want to modify the summary buffer, you can use this hook."
649   :group 'gnus-summary-various
650   :type 'hook)
651
652 (defcustom gnus-summary-prepared-hook nil
653   "*A hook called as the last thing after the summary buffer has been generated."
654   :group 'gnus-summary-various
655   :type 'hook)
656
657 (defcustom gnus-summary-generate-hook nil
658   "*A hook run just before generating the summary buffer.
659 This hook is commonly used to customize threading variables and the
660 like."
661   :group 'gnus-summary-various
662   :type 'hook)
663
664 (defcustom gnus-select-group-hook nil
665   "*A hook called when a newsgroup is selected.
666
667 If you'd like to simplify subjects like the
668 `gnus-summary-next-same-subject' command does, you can use the
669 following hook:
670
671  (setq gnus-select-group-hook
672       (list
673         (lambda ()
674           (mapcar (lambda (header)
675                      (mail-header-set-subject
676                       header
677                       (gnus-simplify-subject
678                        (mail-header-subject header) 're-only)))
679                   gnus-newsgroup-headers))))"
680   :group 'gnus-group-select
681   :type 'hook)
682
683 (defcustom gnus-select-article-hook nil
684   "*A hook called when an article is selected."
685   :group 'gnus-summary-choose
686   :type 'hook)
687
688 (defcustom gnus-visual-mark-article-hook
689   (list 'gnus-highlight-selected-summary)
690   "*Hook run after selecting an article in the summary buffer.
691 It is meant to be used for highlighting the article in some way.  It
692 is not run if `gnus-visual' is nil."
693   :group 'gnus-summary-visual
694   :type 'hook)
695
696 (defcustom gnus-parse-headers-hook nil
697   "*A hook called before parsing the headers."
698   :group 'gnus-various
699   :type 'hook)
700
701 (defcustom gnus-exit-group-hook nil
702   "*A hook called when exiting summary mode.
703 This hook is not called from the non-updating exit commands like `Q'."
704   :group 'gnus-various
705   :type 'hook)
706
707 (defcustom gnus-summary-update-hook
708   (list 'gnus-summary-highlight-line)
709   "*A hook called when a summary line is changed.
710 The hook will not be called if `gnus-visual' is nil.
711
712 The default function `gnus-summary-highlight-line' will
713 highlight the line according to the `gnus-summary-highlight'
714 variable."
715   :group 'gnus-summary-visual
716   :type 'hook)
717
718 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
719   "*A hook called when an article is selected for the first time.
720 The hook is intended to mark an article as read (or unread)
721 automatically when it is selected."
722   :group 'gnus-summary-choose
723   :type 'hook)
724
725 (defcustom gnus-group-no-more-groups-hook nil
726   "*A hook run when returning to group mode having no more (unread) groups."
727   :group 'gnus-group-select
728   :type 'hook)
729
730 (defcustom gnus-ps-print-hook nil
731   "*A hook run before ps-printing something from Gnus."
732   :group 'gnus-summary
733   :type 'hook)
734
735 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
736   "Face used for highlighting the current article in the summary buffer."
737   :group 'gnus-summary-visual
738   :type 'face)
739
740 (defcustom gnus-summary-highlight
741   '(((= mark gnus-canceled-mark)
742      . gnus-summary-cancelled-face)
743     ((and (> score default)
744           (or (= mark gnus-dormant-mark)
745               (= mark gnus-ticked-mark)))
746      . gnus-summary-high-ticked-face)
747     ((and (< score default)
748           (or (= mark gnus-dormant-mark)
749               (= mark gnus-ticked-mark)))
750      . gnus-summary-low-ticked-face)
751     ((or (= mark gnus-dormant-mark)
752          (= mark gnus-ticked-mark))
753      . gnus-summary-normal-ticked-face)
754     ((and (> score default) (= mark gnus-ancient-mark))
755      . gnus-summary-high-ancient-face)
756     ((and (< score default) (= mark gnus-ancient-mark))
757      . gnus-summary-low-ancient-face)
758     ((= mark gnus-ancient-mark)
759      . gnus-summary-normal-ancient-face)
760     ((and (> score default) (= mark gnus-unread-mark))
761      . gnus-summary-high-unread-face)
762     ((and (< score default) (= mark gnus-unread-mark))
763      . gnus-summary-low-unread-face)
764     ((= mark gnus-unread-mark)
765      . gnus-summary-normal-unread-face)
766     ((and (> score default) (memq mark (list gnus-downloadable-mark
767                                              gnus-undownloaded-mark)))
768      . gnus-summary-high-unread-face)
769     ((and (< score default) (memq mark (list gnus-downloadable-mark
770                                              gnus-undownloaded-mark)))
771      . gnus-summary-low-unread-face)
772     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
773      . gnus-summary-normal-unread-face)
774     ((> score default)
775      . gnus-summary-high-read-face)
776     ((< score default)
777      . gnus-summary-low-read-face)
778     (t
779      . gnus-summary-normal-read-face))
780   "*Controls the highlighting of summary buffer lines.
781
782 A list of (FORM . FACE) pairs.  When deciding how a a particular
783 summary line should be displayed, each form is evaluated.  The content
784 of the face field after the first true form is used.  You can change
785 how those summary lines are displayed, by editing the face field.
786
787 You can use the following variables in the FORM field.
788
789 score:   The articles score
790 default: The default article score.
791 below:   The score below which articles are automatically marked as read.
792 mark:    The articles mark."
793   :group 'gnus-summary-visual
794   :type '(repeat (cons (sexp :tag "Form" nil)
795                        face)))
796
797 (defcustom gnus-alter-header-function nil
798   "Function called to allow alteration of article header structures.
799 The function is called with one parameter, the article header vector,
800 which it may alter in any way.")
801
802 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
803   "Variable that says which function should be used to decode a string with encoded words.")
804
805 (defcustom gnus-extra-headers nil
806   "*Extra headers to parse."
807   :group 'gnus-summary
808   :type '(repeat symbol))
809
810 (defcustom gnus-ignored-from-addresses
811   (and user-mail-address (regexp-quote user-mail-address))
812   "*Regexp of From headers that may be suppressed in favor of To headers."
813   :group 'gnus-summary
814   :type 'regexp)
815
816 (defcustom gnus-group-charset-alist
817   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
818     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
819     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
820     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
821     ("^relcom\\>" koi8-r)
822     ("^fido7\\>" koi8-r)
823     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
824     ("^israel\\>" iso-8859-1)
825     ("^han\\>" euc-kr)
826     ("^alt.chinese.text.big5\\>" chinese-big5)
827     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
828     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
829     (".*" iso-8859-1))
830   "Alist of regexps (to match group names) and default charsets to be used when reading."
831   :type '(repeat (list (regexp :tag "Group")
832                        (symbol :tag "Charset")))
833   :group 'gnus-charset)
834
835 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
836   "List of charsets that should be ignored.
837 When these charsets are used in the \"charset\" parameter, the
838 default charset will be used instead."
839   :type '(repeat symbol)
840   :group 'gnus-charset)
841
842 (defcustom gnus-group-ignored-charsets-alist
843   '(("alt\\.chinese\\.text" iso-8859-1))
844   "Alist of regexps (to match group names) and charsets that should be ignored.
845 When these charsets are used in the \"charset\" parameter, the
846 default charset will be used instead."
847   :type '(repeat (cons (regexp :tag "Group")
848                        (repeat symbol)))
849   :group 'gnus-charset)
850
851 (defcustom gnus-group-highlight-words-alist nil
852   "Alist of group regexps and highlight regexps.
853 This variable uses the same syntax as `gnus-emphasis-alist'."
854   :type '(repeat (cons (regexp :tag "Group")
855                        (repeat (list (regexp :tag "Highlight regexp")
856                                      (number :tag "Group for entire word" 0)
857                                      (number :tag "Group for displayed part" 0)
858                                      (symbol :tag "Face"
859                                              gnus-emphasis-highlight-words)))))
860   :group 'gnus-summary-visual)
861
862 (defcustom gnus-summary-show-article-charset-alist
863   nil
864   "Alist of number and charset.
865 The article will be shown with the charset corresponding to the
866 numbered argument.
867 For example: ((1 . cn-gb-2312) (2 . big5))."
868   :type '(repeat (cons (number :tag "Argument" 1)
869                        (symbol :tag "Charset")))
870   :group 'gnus-charset)
871
872 (defcustom gnus-preserve-marks t
873   "Whether marks are preserved when moving, copying and respooling messages."
874   :type 'boolean
875   :group 'gnus-summary-marks)
876
877 (defcustom gnus-alter-articles-to-read-function nil
878   "Function to be called to alter the list of articles to be selected."
879   :type 'function
880   :group 'gnus-summary)
881
882 (defcustom gnus-orphan-score nil
883   "*All orphans get this score added.  Set in the score file."
884   :group 'gnus-score-default
885   :type '(choice (const nil)
886                  integer))
887
888 ;;; Internal variables
889
890 (defvar gnus-article-mime-handles nil)
891 (defvar gnus-article-decoded-p nil)
892 (defvar gnus-scores-exclude-files nil)
893 (defvar gnus-page-broken nil)
894 (defvar gnus-inhibit-mime-unbuttonizing nil)
895
896 (defvar gnus-original-article nil)
897 (defvar gnus-article-internal-prepare-hook nil)
898 (defvar gnus-newsgroup-process-stack nil)
899
900 (defvar gnus-thread-indent-array nil)
901 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
902 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
903   "Function called to sort the articles within a thread after it has been gathered together.")
904
905 ;; Avoid highlighting in kill files.
906 (defvar gnus-summary-inhibit-highlight nil)
907 (defvar gnus-newsgroup-selected-overlay nil)
908 (defvar gnus-inhibit-limiting nil)
909 (defvar gnus-newsgroup-adaptive-score-file nil)
910 (defvar gnus-current-score-file nil)
911 (defvar gnus-current-move-group nil)
912 (defvar gnus-current-copy-group nil)
913 (defvar gnus-current-crosspost-group nil)
914
915 (defvar gnus-newsgroup-dependencies nil)
916 (defvar gnus-newsgroup-adaptive nil)
917 (defvar gnus-summary-display-article-function nil)
918 (defvar gnus-summary-highlight-line-function nil
919   "Function called after highlighting a summary line.")
920
921 (defvar gnus-summary-line-format-alist
922   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
923     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
924     (?s gnus-tmp-subject-or-nil ?s)
925     (?n gnus-tmp-name ?s)
926     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
927         ?s)
928     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
929             gnus-tmp-from) ?s)
930     (?F gnus-tmp-from ?s)
931     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
932     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
933     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
934     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
935     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
936     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
937     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
938     (?L gnus-tmp-lines ?d)
939     (?I gnus-tmp-indentation ?s)
940     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
941     (?R gnus-tmp-replied ?c)
942     (?\[ gnus-tmp-opening-bracket ?c)
943     (?\] gnus-tmp-closing-bracket ?c)
944     (?\> (make-string gnus-tmp-level ? ) ?s)
945     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
946     (?i gnus-tmp-score ?d)
947     (?z gnus-tmp-score-char ?c)
948     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
949     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
950     (?U gnus-tmp-unread ?c)
951     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
952     (?t (gnus-summary-number-of-articles-in-thread
953          (and (boundp 'thread) (car thread)) gnus-tmp-level)
954         ?d)
955     (?e (gnus-summary-number-of-articles-in-thread
956          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
957         ?c)
958     (?u gnus-tmp-user-defined ?s)
959     (?P (gnus-pick-line-number) ?d))
960   "An alist of format specifications that can appear in summary lines.
961 These are paired with what variables they correspond with, along with
962 the type of the variable (string, integer, character, etc).")
963
964 (defvar gnus-summary-dummy-line-format-alist
965   `((?S gnus-tmp-subject ?s)
966     (?N gnus-tmp-number ?d)
967     (?u gnus-tmp-user-defined ?s)))
968
969 (defvar gnus-summary-mode-line-format-alist
970   `((?G gnus-tmp-group-name ?s)
971     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
972     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
973     (?A gnus-tmp-article-number ?d)
974     (?Z gnus-tmp-unread-and-unselected ?s)
975     (?V gnus-version ?s)
976     (?U gnus-tmp-unread-and-unticked ?d)
977     (?S gnus-tmp-subject ?s)
978     (?e gnus-tmp-unselected ?d)
979     (?u gnus-tmp-user-defined ?s)
980     (?d (length gnus-newsgroup-dormant) ?d)
981     (?t (length gnus-newsgroup-marked) ?d)
982     (?r (length gnus-newsgroup-reads) ?d)
983     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
984     (?E gnus-newsgroup-expunged-tally ?d)
985     (?s (gnus-current-score-file-nondirectory) ?s)))
986
987 (defvar gnus-last-search-regexp nil
988   "Default regexp for article search command.")
989
990 (defvar gnus-last-shell-command nil
991   "Default shell command on article.")
992
993 (defvar gnus-newsgroup-begin nil)
994 (defvar gnus-newsgroup-end nil)
995 (defvar gnus-newsgroup-last-rmail nil)
996 (defvar gnus-newsgroup-last-mail nil)
997 (defvar gnus-newsgroup-last-folder nil)
998 (defvar gnus-newsgroup-last-file nil)
999 (defvar gnus-newsgroup-auto-expire nil)
1000 (defvar gnus-newsgroup-active nil)
1001
1002 (defvar gnus-newsgroup-data nil)
1003 (defvar gnus-newsgroup-data-reverse nil)
1004 (defvar gnus-newsgroup-limit nil)
1005 (defvar gnus-newsgroup-limits nil)
1006
1007 (defvar gnus-newsgroup-unreads nil
1008   "List of unread articles in the current newsgroup.")
1009
1010 (defvar gnus-newsgroup-unselected nil
1011   "List of unselected unread articles in the current newsgroup.")
1012
1013 (defvar gnus-newsgroup-reads nil
1014   "Alist of read articles and article marks in the current newsgroup.")
1015
1016 (defvar gnus-newsgroup-expunged-tally nil)
1017
1018 (defvar gnus-newsgroup-marked nil
1019   "List of ticked articles in the current newsgroup (a subset of unread art).")
1020
1021 (defvar gnus-newsgroup-killed nil
1022   "List of ranges of articles that have been through the scoring process.")
1023
1024 (defvar gnus-newsgroup-cached nil
1025   "List of articles that come from the article cache.")
1026
1027 (defvar gnus-newsgroup-saved nil
1028   "List of articles that have been saved.")
1029
1030 (defvar gnus-newsgroup-kill-headers nil)
1031
1032 (defvar gnus-newsgroup-replied nil
1033   "List of articles that have been replied to in the current newsgroup.")
1034
1035 (defvar gnus-newsgroup-expirable nil
1036   "List of articles in the current newsgroup that can be expired.")
1037
1038 (defvar gnus-newsgroup-processable nil
1039   "List of articles in the current newsgroup that can be processed.")
1040
1041 (defvar gnus-newsgroup-downloadable nil
1042   "List of articles in the current newsgroup that can be processed.")
1043
1044 (defvar gnus-newsgroup-undownloaded nil
1045   "List of articles in the current newsgroup that haven't been downloaded..")
1046
1047 (defvar gnus-newsgroup-unsendable nil
1048   "List of articles in the current newsgroup that won't be sent.")
1049
1050 (defvar gnus-newsgroup-bookmarks nil
1051   "List of articles in the current newsgroup that have bookmarks.")
1052
1053 (defvar gnus-newsgroup-dormant nil
1054   "List of dormant articles in the current newsgroup.")
1055
1056 (defvar gnus-newsgroup-scored nil
1057   "List of scored articles in the current newsgroup.")
1058
1059 (defvar gnus-newsgroup-headers nil
1060   "List of article headers in the current newsgroup.")
1061
1062 (defvar gnus-newsgroup-threads nil)
1063
1064 (defvar gnus-newsgroup-prepared nil
1065   "Whether the current group has been prepared properly.")
1066
1067 (defvar gnus-newsgroup-ancient nil
1068   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1069
1070 (defvar gnus-newsgroup-sparse nil)
1071
1072 (defvar gnus-current-article nil)
1073 (defvar gnus-article-current nil)
1074 (defvar gnus-current-headers nil)
1075 (defvar gnus-have-all-headers nil)
1076 (defvar gnus-last-article nil)
1077 (defvar gnus-newsgroup-history nil)
1078 (defvar gnus-newsgroup-charset nil)
1079 (defvar gnus-newsgroup-ephemeral-charset nil)
1080 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1081
1082 (defconst gnus-summary-local-variables
1083   '(gnus-newsgroup-name
1084     gnus-newsgroup-begin gnus-newsgroup-end
1085     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1086     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1087     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1088     gnus-newsgroup-unselected gnus-newsgroup-marked
1089     gnus-newsgroup-reads gnus-newsgroup-saved
1090     gnus-newsgroup-replied gnus-newsgroup-expirable
1091     gnus-newsgroup-processable gnus-newsgroup-killed
1092     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1093     gnus-newsgroup-unsendable
1094     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1095     gnus-newsgroup-headers gnus-newsgroup-threads
1096     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1097     gnus-current-article gnus-current-headers gnus-have-all-headers
1098     gnus-last-article gnus-article-internal-prepare-hook
1099     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1100     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1101     gnus-thread-expunge-below
1102     gnus-score-alist gnus-current-score-file
1103     (gnus-summary-expunge-below . global)
1104     (gnus-summary-mark-below . global)
1105     (gnus-orphan-score . global)
1106     gnus-newsgroup-active gnus-scores-exclude-files
1107     gnus-newsgroup-history gnus-newsgroup-ancient
1108     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1109     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1110     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1111     (gnus-newsgroup-expunged-tally . 0)
1112     gnus-cache-removable-articles gnus-newsgroup-cached
1113     gnus-newsgroup-data gnus-newsgroup-data-reverse
1114     gnus-newsgroup-limit gnus-newsgroup-limits
1115     gnus-newsgroup-charset)
1116   "Variables that are buffer-local to the summary buffers.")
1117
1118 ;; Byte-compiler warning.
1119 (defvar gnus-article-mode-map)
1120
1121 ;; MIME stuff.
1122
1123 (defvar gnus-decode-encoded-word-methods
1124   '(mail-decode-encoded-word-string)
1125   "List of methods used to decode encoded words.
1126
1127 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1128 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1129 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1130 whose names match REGEXP.
1131
1132 For example:
1133 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1134  mail-decode-encoded-word-string
1135  (\"chinese\" . rfc1843-decode-string))")
1136
1137 (defvar gnus-decode-encoded-word-methods-cache nil)
1138
1139 (defun gnus-multi-decode-encoded-word-string (string)
1140   "Apply the functions from `gnus-encoded-word-methods' that match."
1141   (unless (and gnus-decode-encoded-word-methods-cache
1142                (eq gnus-newsgroup-name
1143                    (car gnus-decode-encoded-word-methods-cache)))
1144     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1145     (mapcar (lambda (x)
1146               (if (symbolp x)
1147                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1148                 (if (and gnus-newsgroup-name
1149                          (string-match (car x) gnus-newsgroup-name))
1150                     (nconc gnus-decode-encoded-word-methods-cache
1151                            (list (cdr x))))))
1152           gnus-decode-encoded-word-methods))
1153   (let ((xlist gnus-decode-encoded-word-methods-cache))
1154     (pop xlist)
1155     (while xlist
1156       (setq string (funcall (pop xlist) string))))
1157   string)
1158
1159 ;; Subject simplification.
1160
1161 (defun gnus-simplify-whitespace (str)
1162   "Remove excessive whitespace from STR."
1163   (let ((mystr str))
1164     ;; Multiple spaces.
1165     (while (string-match "[ \t][ \t]+" mystr)
1166       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1167                           " "
1168                           (substring mystr (match-end 0)))))
1169     ;; Leading spaces.
1170     (when (string-match "^[ \t]+" mystr)
1171       (setq mystr (substring mystr (match-end 0))))
1172     ;; Trailing spaces.
1173     (when (string-match "[ \t]+$" mystr)
1174       (setq mystr (substring mystr 0 (match-beginning 0))))
1175     mystr))
1176
1177 (defsubst gnus-simplify-subject-re (subject)
1178   "Remove \"Re:\" from subject lines."
1179   (if (string-match "^[Rr][Ee]: *" subject)
1180       (substring subject (match-end 0))
1181     subject))
1182
1183 (defun gnus-simplify-subject (subject &optional re-only)
1184   "Remove `Re:' and words in parentheses.
1185 If RE-ONLY is non-nil, strip leading `Re:'s only."
1186   (let ((case-fold-search t))           ;Ignore case.
1187     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1188     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1189       (setq subject (substring subject (match-end 0))))
1190     ;; Remove uninteresting prefixes.
1191     (when (and (not re-only)
1192                gnus-simplify-ignored-prefixes
1193                (string-match gnus-simplify-ignored-prefixes subject))
1194       (setq subject (substring subject (match-end 0))))
1195     ;; Remove words in parentheses from end.
1196     (unless re-only
1197       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1198         (setq subject (substring subject 0 (match-beginning 0)))))
1199     ;; Return subject string.
1200     subject))
1201
1202 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1203 ;; all whitespace.
1204 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1205   (goto-char (point-min))
1206   (while (re-search-forward regexp nil t)
1207     (replace-match (or newtext ""))))
1208
1209 (defun gnus-simplify-buffer-fuzzy ()
1210   "Simplify string in the buffer fuzzily.
1211 The string in the accessible portion of the current buffer is simplified.
1212 It is assumed to be a single-line subject.
1213 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1214 matter is removed.  Additional things can be deleted by setting
1215 `gnus-simplify-subject-fuzzy-regexp'."
1216   (let ((case-fold-search t)
1217         (modified-tick))
1218     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1219
1220     (while (not (eq modified-tick (buffer-modified-tick)))
1221       (setq modified-tick (buffer-modified-tick))
1222       (cond
1223        ((listp gnus-simplify-subject-fuzzy-regexp)
1224         (mapcar 'gnus-simplify-buffer-fuzzy-step
1225                 gnus-simplify-subject-fuzzy-regexp))
1226        (gnus-simplify-subject-fuzzy-regexp
1227         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1228       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1229       (gnus-simplify-buffer-fuzzy-step
1230        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1231       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1232
1233     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1234     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1235     (gnus-simplify-buffer-fuzzy-step " $")
1236     (gnus-simplify-buffer-fuzzy-step "^ +")))
1237
1238 (defun gnus-simplify-subject-fuzzy (subject)
1239   "Simplify a subject string fuzzily.
1240 See `gnus-simplify-buffer-fuzzy' for details."
1241   (save-excursion
1242     (gnus-set-work-buffer)
1243     (let ((case-fold-search t))
1244       ;; Remove uninteresting prefixes.
1245       (when (and gnus-simplify-ignored-prefixes
1246                  (string-match gnus-simplify-ignored-prefixes subject))
1247         (setq subject (substring subject (match-end 0))))
1248       (insert subject)
1249       (inline (gnus-simplify-buffer-fuzzy))
1250       (buffer-string))))
1251
1252 (defsubst gnus-simplify-subject-fully (subject)
1253   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1254   (cond
1255    (gnus-simplify-subject-functions
1256     (gnus-map-function gnus-simplify-subject-functions subject))
1257    ((null gnus-summary-gather-subject-limit)
1258     (gnus-simplify-subject-re subject))
1259    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1260     (gnus-simplify-subject-fuzzy subject))
1261    ((numberp gnus-summary-gather-subject-limit)
1262     (gnus-limit-string (gnus-simplify-subject-re subject)
1263                        gnus-summary-gather-subject-limit))
1264    (t
1265     subject)))
1266
1267 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1268   "Check whether two subjects are equal.
1269 If optional argument simple-first is t, first argument is already
1270 simplified."
1271   (cond
1272    ((null simple-first)
1273     (equal (gnus-simplify-subject-fully s1)
1274            (gnus-simplify-subject-fully s2)))
1275    (t
1276     (equal s1
1277            (gnus-simplify-subject-fully s2)))))
1278
1279 (defun gnus-summary-bubble-group ()
1280   "Increase the score of the current group.
1281 This is a handy function to add to `gnus-summary-exit-hook' to
1282 increase the score of each group you read."
1283   (gnus-group-add-score gnus-newsgroup-name))
1284
1285 \f
1286 ;;;
1287 ;;; Gnus summary mode
1288 ;;;
1289
1290 (put 'gnus-summary-mode 'mode-class 'special)
1291
1292 (when t
1293   ;; Non-orthogonal keys
1294
1295   (gnus-define-keys gnus-summary-mode-map
1296     " " gnus-summary-next-page
1297     "\177" gnus-summary-prev-page
1298     [delete] gnus-summary-prev-page
1299     [backspace] gnus-summary-prev-page
1300     "\r" gnus-summary-scroll-up
1301     "\M-\r" gnus-summary-scroll-down
1302     "n" gnus-summary-next-unread-article
1303     "p" gnus-summary-prev-unread-article
1304     "N" gnus-summary-next-article
1305     "P" gnus-summary-prev-article
1306     "\M-\C-n" gnus-summary-next-same-subject
1307     "\M-\C-p" gnus-summary-prev-same-subject
1308     "\M-n" gnus-summary-next-unread-subject
1309     "\M-p" gnus-summary-prev-unread-subject
1310     "." gnus-summary-first-unread-article
1311     "," gnus-summary-best-unread-article
1312     "\M-s" gnus-summary-search-article-forward
1313     "\M-r" gnus-summary-search-article-backward
1314     "<" gnus-summary-beginning-of-article
1315     ">" gnus-summary-end-of-article
1316     "j" gnus-summary-goto-article
1317     "^" gnus-summary-refer-parent-article
1318     "\M-^" gnus-summary-refer-article
1319     "u" gnus-summary-tick-article-forward
1320     "!" gnus-summary-tick-article-forward
1321     "U" gnus-summary-tick-article-backward
1322     "d" gnus-summary-mark-as-read-forward
1323     "D" gnus-summary-mark-as-read-backward
1324     "E" gnus-summary-mark-as-expirable
1325     "\M-u" gnus-summary-clear-mark-forward
1326     "\M-U" gnus-summary-clear-mark-backward
1327     "k" gnus-summary-kill-same-subject-and-select
1328     "\C-k" gnus-summary-kill-same-subject
1329     "\M-\C-k" gnus-summary-kill-thread
1330     "\M-\C-l" gnus-summary-lower-thread
1331     "e" gnus-summary-edit-article
1332     "#" gnus-summary-mark-as-processable
1333     "\M-#" gnus-summary-unmark-as-processable
1334     "\M-\C-t" gnus-summary-toggle-threads
1335     "\M-\C-s" gnus-summary-show-thread
1336     "\M-\C-h" gnus-summary-hide-thread
1337     "\M-\C-f" gnus-summary-next-thread
1338     "\M-\C-b" gnus-summary-prev-thread
1339     [(meta down)] gnus-summary-next-thread
1340     [(meta up)] gnus-summary-prev-thread
1341     "\M-\C-u" gnus-summary-up-thread
1342     "\M-\C-d" gnus-summary-down-thread
1343     "&" gnus-summary-execute-command
1344     "c" gnus-summary-catchup-and-exit
1345     "\C-w" gnus-summary-mark-region-as-read
1346     "\C-t" gnus-summary-toggle-truncation
1347     "?" gnus-summary-mark-as-dormant
1348     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1349     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1350     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1351     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1352     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1353     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1354     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1355     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1356     "=" gnus-summary-expand-window
1357     "\C-x\C-s" gnus-summary-reselect-current-group
1358     "\M-g" gnus-summary-rescan-group
1359     "w" gnus-summary-stop-page-breaking
1360     "\C-c\C-r" gnus-summary-caesar-message
1361     "f" gnus-summary-followup
1362     "F" gnus-summary-followup-with-original
1363     "C" gnus-summary-cancel-article
1364     "r" gnus-summary-reply
1365     "R" gnus-summary-reply-with-original
1366     "\C-c\C-f" gnus-summary-mail-forward
1367     "o" gnus-summary-save-article
1368     "\C-o" gnus-summary-save-article-mail
1369     "|" gnus-summary-pipe-output
1370     "\M-k" gnus-summary-edit-local-kill
1371     "\M-K" gnus-summary-edit-global-kill
1372     ;; "V" gnus-version
1373     "\C-c\C-d" gnus-summary-describe-group
1374     "q" gnus-summary-exit
1375     "Q" gnus-summary-exit-no-update
1376     "\C-c\C-i" gnus-info-find-node
1377     gnus-mouse-2 gnus-mouse-pick-article
1378     "m" gnus-summary-mail-other-window
1379     "a" gnus-summary-post-news
1380     "x" gnus-summary-limit-to-unread
1381     "s" gnus-summary-isearch-article
1382     "t" gnus-summary-toggle-header
1383     "g" gnus-summary-show-article
1384     "l" gnus-summary-goto-last-article
1385     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1386     "\C-d" gnus-summary-enter-digest-group
1387     "\M-\C-d" gnus-summary-read-document
1388     "\M-\C-e" gnus-summary-edit-parameters
1389     "\M-\C-a" gnus-summary-customize-parameters
1390     "\C-c\C-b" gnus-bug
1391     "*" gnus-cache-enter-article
1392     "\M-*" gnus-cache-remove-article
1393     "\M-&" gnus-summary-universal-argument
1394     "\C-l" gnus-recenter
1395     "I" gnus-summary-increase-score
1396     "L" gnus-summary-lower-score
1397     "\M-i" gnus-symbolic-argument
1398     "h" gnus-summary-select-article-buffer
1399
1400     "b" gnus-article-view-part
1401     "\M-t" gnus-summary-toggle-display-buttonized
1402
1403     "V" gnus-summary-score-map
1404     "X" gnus-uu-extract-map
1405     "S" gnus-summary-send-map)
1406
1407   ;; Sort of orthogonal keymap
1408   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1409     "t" gnus-summary-tick-article-forward
1410     "!" gnus-summary-tick-article-forward
1411     "d" gnus-summary-mark-as-read-forward
1412     "r" gnus-summary-mark-as-read-forward
1413     "c" gnus-summary-clear-mark-forward
1414     " " gnus-summary-clear-mark-forward
1415     "e" gnus-summary-mark-as-expirable
1416     "x" gnus-summary-mark-as-expirable
1417     "?" gnus-summary-mark-as-dormant
1418     "b" gnus-summary-set-bookmark
1419     "B" gnus-summary-remove-bookmark
1420     "#" gnus-summary-mark-as-processable
1421     "\M-#" gnus-summary-unmark-as-processable
1422     "S" gnus-summary-limit-include-expunged
1423     "C" gnus-summary-catchup
1424     "H" gnus-summary-catchup-to-here
1425     "\C-c" gnus-summary-catchup-all
1426     "k" gnus-summary-kill-same-subject-and-select
1427     "K" gnus-summary-kill-same-subject
1428     "P" gnus-uu-mark-map)
1429
1430   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1431     "c" gnus-summary-clear-above
1432     "u" gnus-summary-tick-above
1433     "m" gnus-summary-mark-above
1434     "k" gnus-summary-kill-below)
1435
1436   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1437     "/" gnus-summary-limit-to-subject
1438     "n" gnus-summary-limit-to-articles
1439     "w" gnus-summary-pop-limit
1440     "s" gnus-summary-limit-to-subject
1441     "a" gnus-summary-limit-to-author
1442     "u" gnus-summary-limit-to-unread
1443     "m" gnus-summary-limit-to-marks
1444     "M" gnus-summary-limit-exclude-marks
1445     "v" gnus-summary-limit-to-score
1446     "*" gnus-summary-limit-include-cached
1447     "D" gnus-summary-limit-include-dormant
1448     "T" gnus-summary-limit-include-thread
1449     "d" gnus-summary-limit-exclude-dormant
1450     "t" gnus-summary-limit-to-age
1451     "x" gnus-summary-limit-to-extra
1452     "E" gnus-summary-limit-include-expunged
1453     "c" gnus-summary-limit-exclude-childless-dormant
1454     "C" gnus-summary-limit-mark-excluded-as-read)
1455
1456   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1457     "n" gnus-summary-next-unread-article
1458     "p" gnus-summary-prev-unread-article
1459     "N" gnus-summary-next-article
1460     "P" gnus-summary-prev-article
1461     "\C-n" gnus-summary-next-same-subject
1462     "\C-p" gnus-summary-prev-same-subject
1463     "\M-n" gnus-summary-next-unread-subject
1464     "\M-p" gnus-summary-prev-unread-subject
1465     "f" gnus-summary-first-unread-article
1466     "b" gnus-summary-best-unread-article
1467     "j" gnus-summary-goto-article
1468     "g" gnus-summary-goto-subject
1469     "l" gnus-summary-goto-last-article
1470     "o" gnus-summary-pop-article)
1471
1472   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1473     "k" gnus-summary-kill-thread
1474     "l" gnus-summary-lower-thread
1475     "i" gnus-summary-raise-thread
1476     "T" gnus-summary-toggle-threads
1477     "t" gnus-summary-rethread-current
1478     "^" gnus-summary-reparent-thread
1479     "s" gnus-summary-show-thread
1480     "S" gnus-summary-show-all-threads
1481     "h" gnus-summary-hide-thread
1482     "H" gnus-summary-hide-all-threads
1483     "n" gnus-summary-next-thread
1484     "p" gnus-summary-prev-thread
1485     "u" gnus-summary-up-thread
1486     "o" gnus-summary-top-thread
1487     "d" gnus-summary-down-thread
1488     "#" gnus-uu-mark-thread
1489     "\M-#" gnus-uu-unmark-thread)
1490
1491   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1492     "g" gnus-summary-prepare
1493     "c" gnus-summary-insert-cached-articles)
1494
1495   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1496     "c" gnus-summary-catchup-and-exit
1497     "C" gnus-summary-catchup-all-and-exit
1498     "E" gnus-summary-exit-no-update
1499     "Q" gnus-summary-exit
1500     "Z" gnus-summary-exit
1501     "n" gnus-summary-catchup-and-goto-next-group
1502     "R" gnus-summary-reselect-current-group
1503     "G" gnus-summary-rescan-group
1504     "N" gnus-summary-next-group
1505     "s" gnus-summary-save-newsrc
1506     "P" gnus-summary-prev-group)
1507
1508   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1509     " " gnus-summary-next-page
1510     "n" gnus-summary-next-page
1511     "\177" gnus-summary-prev-page
1512     [delete] gnus-summary-prev-page
1513     "p" gnus-summary-prev-page
1514     "\r" gnus-summary-scroll-up
1515     "\M-\r" gnus-summary-scroll-down
1516     "<" gnus-summary-beginning-of-article
1517     ">" gnus-summary-end-of-article
1518     "b" gnus-summary-beginning-of-article
1519     "e" gnus-summary-end-of-article
1520     "^" gnus-summary-refer-parent-article
1521     "r" gnus-summary-refer-parent-article
1522     "D" gnus-summary-enter-digest-group
1523     "R" gnus-summary-refer-references
1524     "T" gnus-summary-refer-thread
1525     "g" gnus-summary-show-article
1526     "s" gnus-summary-isearch-article
1527     "P" gnus-summary-print-article
1528     "t" gnus-article-babel)
1529
1530   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1531     "b" gnus-article-add-buttons
1532     "B" gnus-article-add-buttons-to-head
1533     "o" gnus-article-treat-overstrike
1534     "e" gnus-article-emphasize
1535     "w" gnus-article-fill-cited-article
1536     "Q" gnus-article-fill-long-lines
1537     "C" gnus-article-capitalize-sentences
1538     "c" gnus-article-remove-cr
1539     "q" gnus-article-de-quoted-unreadable
1540     "6" gnus-article-de-base64-unreadable
1541     "Z" gnus-article-decode-HZ
1542     "h" gnus-article-wash-html
1543     "f" gnus-article-display-x-face
1544     "l" gnus-summary-stop-page-breaking
1545     "r" gnus-summary-caesar-message
1546     "t" gnus-summary-toggle-header
1547     "v" gnus-summary-verbose-headers
1548     "H" gnus-article-strip-headers-in-body
1549     "d" gnus-article-treat-dumbquotes)
1550
1551   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1552     "a" gnus-article-hide
1553     "h" gnus-article-hide-headers
1554     "b" gnus-article-hide-boring-headers
1555     "s" gnus-article-hide-signature
1556     "c" gnus-article-hide-citation
1557     "C" gnus-article-hide-citation-in-followups
1558     "l" gnus-article-hide-list-identifiers
1559     "p" gnus-article-hide-pgp
1560     "B" gnus-article-strip-banner
1561     "P" gnus-article-hide-pem
1562     "\C-c" gnus-article-hide-citation-maybe)
1563
1564   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1565     "a" gnus-article-highlight
1566     "h" gnus-article-highlight-headers
1567     "c" gnus-article-highlight-citation
1568     "s" gnus-article-highlight-signature)
1569
1570   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1571     "w" gnus-article-decode-mime-words
1572     "c" gnus-article-decode-charset
1573     "v" gnus-mime-view-all-parts
1574     "b" gnus-article-view-part)
1575
1576   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1577     "z" gnus-article-date-ut
1578     "u" gnus-article-date-ut
1579     "l" gnus-article-date-local
1580     "e" gnus-article-date-lapsed
1581     "o" gnus-article-date-original
1582     "i" gnus-article-date-iso8601
1583     "s" gnus-article-date-user)
1584
1585   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1586     "t" gnus-article-remove-trailing-blank-lines
1587     "l" gnus-article-strip-leading-blank-lines
1588     "m" gnus-article-strip-multiple-blank-lines
1589     "a" gnus-article-strip-blank-lines
1590     "A" gnus-article-strip-all-blank-lines
1591     "s" gnus-article-strip-leading-space
1592     "e" gnus-article-strip-trailing-space)
1593
1594   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1595     "v" gnus-version
1596     "f" gnus-summary-fetch-faq
1597     "d" gnus-summary-describe-group
1598     "h" gnus-summary-describe-briefly
1599     "i" gnus-info-find-node)
1600
1601   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1602     "e" gnus-summary-expire-articles
1603     "\M-\C-e" gnus-summary-expire-articles-now
1604     "\177" gnus-summary-delete-article
1605     [delete] gnus-summary-delete-article
1606     [backspace] gnus-summary-delete-article
1607     "m" gnus-summary-move-article
1608     "r" gnus-summary-respool-article
1609     "w" gnus-summary-edit-article
1610     "c" gnus-summary-copy-article
1611     "B" gnus-summary-crosspost-article
1612     "q" gnus-summary-respool-query
1613     "t" gnus-summary-respool-trace
1614     "i" gnus-summary-import-article
1615     "p" gnus-summary-article-posted-p)
1616
1617   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1618     "o" gnus-summary-save-article
1619     "m" gnus-summary-save-article-mail
1620     "F" gnus-summary-write-article-file
1621     "r" gnus-summary-save-article-rmail
1622     "f" gnus-summary-save-article-file
1623     "b" gnus-summary-save-article-body-file
1624     "h" gnus-summary-save-article-folder
1625     "v" gnus-summary-save-article-vm
1626     "p" gnus-summary-pipe-output
1627     "s" gnus-soup-add-article)
1628
1629   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1630     "b" gnus-summary-display-buttonized
1631     "m" gnus-summary-repair-multipart
1632     "v" gnus-article-view-part
1633     "o" gnus-article-save-part
1634     "c" gnus-article-copy-part
1635     "e" gnus-article-externalize-part
1636     "i" gnus-article-inline-part
1637     "|" gnus-article-pipe-part))
1638
1639 (defun gnus-summary-make-menu-bar ()
1640   (gnus-turn-off-edit-menu 'summary)
1641
1642   (unless (boundp 'gnus-summary-misc-menu)
1643
1644     (easy-menu-define
1645      gnus-summary-kill-menu gnus-summary-mode-map ""
1646      (cons
1647       "Score"
1648       (nconc
1649        (list
1650         ["Enter score..." gnus-summary-score-entry t]
1651         ["Customize" gnus-score-customize t])
1652        (gnus-make-score-map 'increase)
1653        (gnus-make-score-map 'lower)
1654        '(("Mark"
1655           ["Kill below" gnus-summary-kill-below t]
1656           ["Mark above" gnus-summary-mark-above t]
1657           ["Tick above" gnus-summary-tick-above t]
1658           ["Clear above" gnus-summary-clear-above t])
1659          ["Current score" gnus-summary-current-score t]
1660          ["Set score" gnus-summary-set-score t]
1661          ["Switch current score file..." gnus-score-change-score-file t]
1662          ["Set mark below..." gnus-score-set-mark-below t]
1663          ["Set expunge below..." gnus-score-set-expunge-below t]
1664          ["Edit current score file" gnus-score-edit-current-scores t]
1665          ["Edit score file" gnus-score-edit-file t]
1666          ["Trace score" gnus-score-find-trace t]
1667          ["Find words" gnus-score-find-favourite-words t]
1668          ["Rescore buffer" gnus-summary-rescore t]
1669          ["Increase score..." gnus-summary-increase-score t]
1670          ["Lower score..." gnus-summary-lower-score t]))))
1671
1672     ;; Define both the Article menu in the summary buffer and the equivalent
1673     ;; Commands menu in the article buffer here for consistency.
1674     (let ((innards
1675            '(("Hide"
1676               ["All" gnus-article-hide t]
1677               ["Headers" gnus-article-hide-headers t]
1678               ["Signature" gnus-article-hide-signature t]
1679               ["Citation" gnus-article-hide-citation t]
1680               ["List identifiers" gnus-article-hide-list-identifiers t]
1681               ["PGP" gnus-article-hide-pgp t]
1682               ["Banner" gnus-article-strip-banner t]
1683               ["Boring headers" gnus-article-hide-boring-headers t])
1684              ("Highlight"
1685               ["All" gnus-article-highlight t]
1686               ["Headers" gnus-article-highlight-headers t]
1687               ["Signature" gnus-article-highlight-signature t]
1688               ["Citation" gnus-article-highlight-citation t])
1689              ("MIME"
1690               ["Words" gnus-article-decode-mime-words t]
1691               ["Charset" gnus-article-decode-charset t]
1692               ["QP" gnus-article-de-quoted-unreadable t]
1693               ["Base64" gnus-article-de-base64-unreadable t]
1694               ["View all" gnus-mime-view-all-parts t])
1695              ("Date"
1696               ["Local" gnus-article-date-local t]
1697               ["ISO8601" gnus-article-date-iso8601 t]
1698               ["UT" gnus-article-date-ut t]
1699               ["Original" gnus-article-date-original t]
1700               ["Lapsed" gnus-article-date-lapsed t]
1701               ["User-defined" gnus-article-date-user t])
1702              ("Washing"
1703               ("Remove Blanks"
1704                ["Leading" gnus-article-strip-leading-blank-lines t]
1705                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1706                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1707                ["All of the above" gnus-article-strip-blank-lines t]
1708                ["All" gnus-article-strip-all-blank-lines t]
1709                ["Leading space" gnus-article-strip-leading-space t]
1710                ["Trailing space" gnus-article-strip-trailing-space t])
1711               ["Overstrike" gnus-article-treat-overstrike t]
1712               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1713               ["Emphasis" gnus-article-emphasize t]
1714               ["Word wrap" gnus-article-fill-cited-article t]
1715               ["Fill long lines" gnus-article-fill-long-lines t]
1716               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1717               ["CR" gnus-article-remove-cr t]
1718               ["Show X-Face" gnus-article-display-x-face t]
1719               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1720               ["Base64" gnus-article-de-base64-unreadable t]
1721               ["Rot 13" gnus-summary-caesar-message t]
1722               ["Unix pipe" gnus-summary-pipe-message t]
1723               ["Add buttons" gnus-article-add-buttons t]
1724               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1725               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1726               ["Verbose header" gnus-summary-verbose-headers t]
1727               ["Toggle header" gnus-summary-toggle-header t]
1728               ["Html" gnus-article-wash-html t]
1729               ["HZ" gnus-article-decode-HZ t])
1730              ("Output"
1731               ["Save in default format" gnus-summary-save-article t]
1732               ["Save in file" gnus-summary-save-article-file t]
1733               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1734               ["Save in MH folder" gnus-summary-save-article-folder t]
1735               ["Save in VM folder" gnus-summary-save-article-vm t]
1736               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1737               ["Save body in file" gnus-summary-save-article-body-file t]
1738               ["Pipe through a filter" gnus-summary-pipe-output t]
1739               ["Add to SOUP packet" gnus-soup-add-article t]
1740               ["Print" gnus-summary-print-article t])
1741              ("Backend"
1742               ["Respool article..." gnus-summary-respool-article t]
1743               ["Move article..." gnus-summary-move-article
1744                (gnus-check-backend-function
1745                 'request-move-article gnus-newsgroup-name)]
1746               ["Copy article..." gnus-summary-copy-article t]
1747               ["Crosspost article..." gnus-summary-crosspost-article
1748                (gnus-check-backend-function
1749                 'request-replace-article gnus-newsgroup-name)]
1750               ["Import file..." gnus-summary-import-article t]
1751               ["Check if posted" gnus-summary-article-posted-p t]
1752               ["Edit article" gnus-summary-edit-article
1753                (not (gnus-group-read-only-p))]
1754               ["Delete article" gnus-summary-delete-article
1755                (gnus-check-backend-function
1756                 'request-expire-articles gnus-newsgroup-name)]
1757               ["Query respool" gnus-summary-respool-query t]
1758               ["Trace respool" gnus-summary-respool-trace t]
1759               ["Delete expirable articles" gnus-summary-expire-articles-now
1760                (gnus-check-backend-function
1761                 'request-expire-articles gnus-newsgroup-name)])
1762              ("Extract"
1763               ["Uudecode" gnus-uu-decode-uu t]
1764               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1765               ["Unshar" gnus-uu-decode-unshar t]
1766               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1767               ["Save" gnus-uu-decode-save t]
1768               ["Binhex" gnus-uu-decode-binhex t]
1769               ["Postscript" gnus-uu-decode-postscript t])
1770              ("Cache"
1771               ["Enter article" gnus-cache-enter-article t]
1772               ["Remove article" gnus-cache-remove-article t])
1773              ["Translate" gnus-article-babel t]
1774              ["Select article buffer" gnus-summary-select-article-buffer t]
1775              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1776              ["Isearch article..." gnus-summary-isearch-article t]
1777              ["Beginning of the article" gnus-summary-beginning-of-article t]
1778              ["End of the article" gnus-summary-end-of-article t]
1779              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1780              ["Fetch referenced articles" gnus-summary-refer-references t]
1781              ["Fetch current thread" gnus-summary-refer-thread t]
1782              ["Fetch article with id..." gnus-summary-refer-article t]
1783              ["Redisplay" gnus-summary-show-article t])))
1784       (easy-menu-define
1785        gnus-summary-article-menu gnus-summary-mode-map ""
1786        (cons "Article" innards))
1787
1788       (easy-menu-define
1789        gnus-article-commands-menu gnus-article-mode-map ""
1790        (cons "Commands" innards)))
1791
1792     (easy-menu-define
1793      gnus-summary-thread-menu gnus-summary-mode-map ""
1794      '("Threads"
1795        ["Toggle threading" gnus-summary-toggle-threads t]
1796        ["Hide threads" gnus-summary-hide-all-threads t]
1797        ["Show threads" gnus-summary-show-all-threads t]
1798        ["Hide thread" gnus-summary-hide-thread t]
1799        ["Show thread" gnus-summary-show-thread t]
1800        ["Go to next thread" gnus-summary-next-thread t]
1801        ["Go to previous thread" gnus-summary-prev-thread t]
1802        ["Go down thread" gnus-summary-down-thread t]
1803        ["Go up thread" gnus-summary-up-thread t]
1804        ["Top of thread" gnus-summary-top-thread t]
1805        ["Mark thread as read" gnus-summary-kill-thread t]
1806        ["Lower thread score" gnus-summary-lower-thread t]
1807        ["Raise thread score" gnus-summary-raise-thread t]
1808        ["Rethread current" gnus-summary-rethread-current t]))
1809
1810     (easy-menu-define
1811      gnus-summary-post-menu gnus-summary-mode-map ""
1812      '("Post"
1813        ["Post an article" gnus-summary-post-news t]
1814        ["Followup" gnus-summary-followup t]
1815        ["Followup and yank" gnus-summary-followup-with-original t]
1816        ["Supersede article" gnus-summary-supersede-article t]
1817        ["Cancel article" gnus-summary-cancel-article t]
1818        ["Reply" gnus-summary-reply t]
1819        ["Reply and yank" gnus-summary-reply-with-original t]
1820        ["Wide reply" gnus-summary-wide-reply t]
1821        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1822        ["Mail forward" gnus-summary-mail-forward t]
1823        ["Post forward" gnus-summary-post-forward t]
1824        ["Digest and mail" gnus-uu-digest-mail-forward t]
1825        ["Digest and post" gnus-uu-digest-post-forward t]
1826        ["Resend message" gnus-summary-resend-message t]
1827        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1828        ["Send a mail" gnus-summary-mail-other-window t]
1829        ["Uuencode and post" gnus-uu-post-news t]
1830        ["Followup via news" gnus-summary-followup-to-mail t]
1831        ["Followup via news and yank"
1832         gnus-summary-followup-to-mail-with-original t]
1833        ;;("Draft"
1834        ;;["Send" gnus-summary-send-draft t]
1835        ;;["Send bounced" gnus-resend-bounced-mail t])
1836        ))
1837
1838     (easy-menu-define
1839      gnus-summary-misc-menu gnus-summary-mode-map ""
1840      '("Misc"
1841        ("Mark Read"
1842         ["Mark as read" gnus-summary-mark-as-read-forward t]
1843         ["Mark same subject and select"
1844          gnus-summary-kill-same-subject-and-select t]
1845         ["Mark same subject" gnus-summary-kill-same-subject t]
1846         ["Catchup" gnus-summary-catchup t]
1847         ["Catchup all" gnus-summary-catchup-all t]
1848         ["Catchup to here" gnus-summary-catchup-to-here t]
1849         ["Catchup region" gnus-summary-mark-region-as-read t]
1850         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1851        ("Mark Various"
1852         ["Tick" gnus-summary-tick-article-forward t]
1853         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1854         ["Remove marks" gnus-summary-clear-mark-forward t]
1855         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1856         ["Set bookmark" gnus-summary-set-bookmark t]
1857         ["Remove bookmark" gnus-summary-remove-bookmark t])
1858        ("Mark Limit"
1859         ["Marks..." gnus-summary-limit-to-marks t]
1860         ["Subject..." gnus-summary-limit-to-subject t]
1861         ["Author..." gnus-summary-limit-to-author t]
1862         ["Age..." gnus-summary-limit-to-age t]
1863         ["Extra..." gnus-summary-limit-to-extra t]
1864         ["Score" gnus-summary-limit-to-score t]
1865         ["Unread" gnus-summary-limit-to-unread t]
1866         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1867         ["Articles" gnus-summary-limit-to-articles t]
1868         ["Pop limit" gnus-summary-pop-limit t]
1869         ["Show dormant" gnus-summary-limit-include-dormant t]
1870         ["Hide childless dormant"
1871          gnus-summary-limit-exclude-childless-dormant t]
1872         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1873         ["Hide marked" gnus-summary-limit-exclude-marks t]
1874         ["Show expunged" gnus-summary-show-all-expunged t])
1875        ("Process Mark"
1876         ["Set mark" gnus-summary-mark-as-processable t]
1877         ["Remove mark" gnus-summary-unmark-as-processable t]
1878         ["Remove all marks" gnus-summary-unmark-all-processable t]
1879         ["Mark above" gnus-uu-mark-over t]
1880         ["Mark series" gnus-uu-mark-series t]
1881         ["Mark region" gnus-uu-mark-region t]
1882         ["Unmark region" gnus-uu-unmark-region t]
1883         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1884         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1885         ["Mark all" gnus-uu-mark-all t]
1886         ["Mark buffer" gnus-uu-mark-buffer t]
1887         ["Mark sparse" gnus-uu-mark-sparse t]
1888         ["Mark thread" gnus-uu-mark-thread t]
1889         ["Unmark thread" gnus-uu-unmark-thread t]
1890         ("Process Mark Sets"
1891          ["Kill" gnus-summary-kill-process-mark t]
1892          ["Yank" gnus-summary-yank-process-mark
1893           gnus-newsgroup-process-stack]
1894          ["Save" gnus-summary-save-process-mark t]))
1895        ("Scroll article"
1896         ["Page forward" gnus-summary-next-page t]
1897         ["Page backward" gnus-summary-prev-page t]
1898         ["Line forward" gnus-summary-scroll-up t])
1899        ("Move"
1900         ["Next unread article" gnus-summary-next-unread-article t]
1901         ["Previous unread article" gnus-summary-prev-unread-article t]
1902         ["Next article" gnus-summary-next-article t]
1903         ["Previous article" gnus-summary-prev-article t]
1904         ["Next unread subject" gnus-summary-next-unread-subject t]
1905         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1906         ["Next article same subject" gnus-summary-next-same-subject t]
1907         ["Previous article same subject" gnus-summary-prev-same-subject t]
1908         ["First unread article" gnus-summary-first-unread-article t]
1909         ["Best unread article" gnus-summary-best-unread-article t]
1910         ["Go to subject number..." gnus-summary-goto-subject t]
1911         ["Go to article number..." gnus-summary-goto-article t]
1912         ["Go to the last article" gnus-summary-goto-last-article t]
1913         ["Pop article off history" gnus-summary-pop-article t])
1914        ("Sort"
1915         ["Sort by number" gnus-summary-sort-by-number t]
1916         ["Sort by author" gnus-summary-sort-by-author t]
1917         ["Sort by subject" gnus-summary-sort-by-subject t]
1918         ["Sort by date" gnus-summary-sort-by-date t]
1919         ["Sort by score" gnus-summary-sort-by-score t]
1920         ["Sort by lines" gnus-summary-sort-by-lines t]
1921         ["Sort by characters" gnus-summary-sort-by-chars t])
1922        ("Help"
1923         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1924         ["Describe group" gnus-summary-describe-group t]
1925         ["Read manual" gnus-info-find-node t])
1926        ("Modes"
1927         ["Pick and read" gnus-pick-mode t]
1928         ["Binary" gnus-binary-mode t])
1929        ("Regeneration"
1930         ["Regenerate" gnus-summary-prepare t]
1931         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1932         ["Toggle threading" gnus-summary-toggle-threads t])
1933        ["Filter articles..." gnus-summary-execute-command t]
1934        ["Run command on subjects..." gnus-summary-universal-argument t]
1935        ["Search articles forward..." gnus-summary-search-article-forward t]
1936        ["Search articles backward..." gnus-summary-search-article-backward t]
1937        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1938        ["Expand window" gnus-summary-expand-window t]
1939        ["Expire expirable articles" gnus-summary-expire-articles
1940         (gnus-check-backend-function
1941          'request-expire-articles gnus-newsgroup-name)]
1942        ["Edit local kill file" gnus-summary-edit-local-kill t]
1943        ["Edit main kill file" gnus-summary-edit-global-kill t]
1944        ["Edit group parameters" gnus-summary-edit-parameters t]
1945        ["Customize group parameters" gnus-summary-customize-parameters t]
1946        ["Send a bug report" gnus-bug t]
1947        ("Exit"
1948         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1949         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1950         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1951         ["Exit group" gnus-summary-exit t]
1952         ["Exit group without updating" gnus-summary-exit-no-update t]
1953         ["Exit and goto next group" gnus-summary-next-group t]
1954         ["Exit and goto prev group" gnus-summary-prev-group t]
1955         ["Reselect group" gnus-summary-reselect-current-group t]
1956         ["Rescan group" gnus-summary-rescan-group t]
1957         ["Update dribble" gnus-summary-save-newsrc t])))
1958
1959     (gnus-run-hooks 'gnus-summary-menu-hook)))
1960
1961 (defun gnus-score-set-default (var value)
1962   "A version of set that updates the GNU Emacs menu-bar."
1963   (set var value)
1964   ;; It is the message that forces the active status to be updated.
1965   (message ""))
1966
1967 (defun gnus-make-score-map (type)
1968   "Make a summary score map of type TYPE."
1969   (if t
1970       nil
1971     (let ((headers '(("author" "from" string)
1972                      ("subject" "subject" string)
1973                      ("article body" "body" string)
1974                      ("article head" "head" string)
1975                      ("xref" "xref" string)
1976                      ("extra header" "extra" string)
1977                      ("lines" "lines" number)
1978                      ("followups to author" "followup" string)))
1979           (types '((number ("less than" <)
1980                            ("greater than" >)
1981                            ("equal" =))
1982                    (string ("substring" s)
1983                            ("exact string" e)
1984                            ("fuzzy string" f)
1985                            ("regexp" r))))
1986           (perms '(("temporary" (current-time-string))
1987                    ("permanent" nil)
1988                    ("immediate" now)))
1989           header)
1990       (list
1991        (apply
1992         'nconc
1993         (list
1994          (if (eq type 'lower)
1995              "Lower score"
1996            "Increase score"))
1997         (let (outh)
1998           (while headers
1999             (setq header (car headers))
2000             (setq outh
2001                   (cons
2002                    (apply
2003                     'nconc
2004                     (list (car header))
2005                     (let ((ts (cdr (assoc (nth 2 header) types)))
2006                           outt)
2007                       (while ts
2008                         (setq outt
2009                               (cons
2010                                (apply
2011                                 'nconc
2012                                 (list (caar ts))
2013                                 (let ((ps perms)
2014                                       outp)
2015                                   (while ps
2016                                     (setq outp
2017                                           (cons
2018                                            (vector
2019                                             (caar ps)
2020                                             (list
2021                                              'gnus-summary-score-entry
2022                                              (nth 1 header)
2023                                              (if (or (string= (nth 1 header)
2024                                                               "head")
2025                                                      (string= (nth 1 header)
2026                                                               "body"))
2027                                                  ""
2028                                                (list 'gnus-summary-header
2029                                                      (nth 1 header)))
2030                                              (list 'quote (nth 1 (car ts)))
2031                                              (list 'gnus-score-delta-default
2032                                                    nil)
2033                                              (nth 1 (car ps))
2034                                              t)
2035                                             t)
2036                                            outp))
2037                                     (setq ps (cdr ps)))
2038                                   (list (nreverse outp))))
2039                                outt))
2040                         (setq ts (cdr ts)))
2041                       (list (nreverse outt))))
2042                    outh))
2043             (setq headers (cdr headers)))
2044           (list (nreverse outh))))))))
2045
2046 \f
2047
2048 (defun gnus-summary-mode (&optional group)
2049   "Major mode for reading articles.
2050
2051 All normal editing commands are switched off.
2052 \\<gnus-summary-mode-map>
2053 Each line in this buffer represents one article.  To read an
2054 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2055 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2056 respectively.
2057
2058 You can also post articles and send mail from this buffer.  To
2059 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2060 of an article, type `\\[gnus-summary-reply]'.
2061
2062 There are approx. one gazillion commands you can execute in this
2063 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2064
2065 The following commands are available:
2066
2067 \\{gnus-summary-mode-map}"
2068   (interactive)
2069   (when (gnus-visual-p 'summary-menu 'menu)
2070     (gnus-summary-make-menu-bar))
2071   (kill-all-local-variables)
2072   (gnus-summary-make-local-variables)
2073   (gnus-make-thread-indent-array)
2074   (gnus-simplify-mode-line)
2075   (setq major-mode 'gnus-summary-mode)
2076   (setq mode-name "Summary")
2077   (make-local-variable 'minor-mode-alist)
2078   (use-local-map gnus-summary-mode-map)
2079   (buffer-disable-undo)
2080   (setq buffer-read-only t)             ;Disable modification
2081   (setq truncate-lines t)
2082   (setq selective-display t)
2083   (setq selective-display-ellipses t)   ;Display `...'
2084   (gnus-summary-set-display-table)
2085   (gnus-set-default-directory)
2086   (setq gnus-newsgroup-name group)
2087   (make-local-variable 'gnus-summary-line-format)
2088   (make-local-variable 'gnus-summary-line-format-spec)
2089   (make-local-variable 'gnus-summary-dummy-line-format)
2090   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2091   (make-local-variable 'gnus-summary-mark-positions)
2092   (make-local-hook 'pre-command-hook)
2093   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2094   (gnus-run-hooks 'gnus-summary-mode-hook)
2095   (mm-enable-multibyte)
2096   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2097   (gnus-update-summary-mark-positions))
2098
2099 (defun gnus-summary-make-local-variables ()
2100   "Make all the local summary buffer variables."
2101   (let (global)
2102     (dolist (local gnus-summary-local-variables)
2103       (if (consp local)
2104           (progn
2105             (if (eq (cdr local) 'global)
2106                 ;; Copy the global value of the variable.
2107                 (setq global (symbol-value (car local)))
2108               ;; Use the value from the list.
2109               (setq global (eval (cdr local))))
2110             (set (make-local-variable (car local)) global))
2111         ;; Simple nil-valued local variable.
2112         (set (make-local-variable local) nil)))))
2113
2114 (defun gnus-summary-clear-local-variables ()
2115   (let ((locals gnus-summary-local-variables))
2116     (while locals
2117       (if (consp (car locals))
2118           (and (vectorp (caar locals))
2119                (set (caar locals) nil))
2120         (and (vectorp (car locals))
2121              (set (car locals) nil)))
2122       (setq locals (cdr locals)))))
2123
2124 ;; Summary data functions.
2125
2126 (defmacro gnus-data-number (data)
2127   `(car ,data))
2128
2129 (defmacro gnus-data-set-number (data number)
2130   `(setcar ,data ,number))
2131
2132 (defmacro gnus-data-mark (data)
2133   `(nth 1 ,data))
2134
2135 (defmacro gnus-data-set-mark (data mark)
2136   `(setcar (nthcdr 1 ,data) ,mark))
2137
2138 (defmacro gnus-data-pos (data)
2139   `(nth 2 ,data))
2140
2141 (defmacro gnus-data-set-pos (data pos)
2142   `(setcar (nthcdr 2 ,data) ,pos))
2143
2144 (defmacro gnus-data-header (data)
2145   `(nth 3 ,data))
2146
2147 (defmacro gnus-data-set-header (data header)
2148   `(setf (nth 3 ,data) ,header))
2149
2150 (defmacro gnus-data-level (data)
2151   `(nth 4 ,data))
2152
2153 (defmacro gnus-data-unread-p (data)
2154   `(= (nth 1 ,data) gnus-unread-mark))
2155
2156 (defmacro gnus-data-read-p (data)
2157   `(/= (nth 1 ,data) gnus-unread-mark))
2158
2159 (defmacro gnus-data-pseudo-p (data)
2160   `(consp (nth 3 ,data)))
2161
2162 (defmacro gnus-data-find (number)
2163   `(assq ,number gnus-newsgroup-data))
2164
2165 (defmacro gnus-data-find-list (number &optional data)
2166   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2167      (memq (assq ,number bdata)
2168            bdata)))
2169
2170 (defmacro gnus-data-make (number mark pos header level)
2171   `(list ,number ,mark ,pos ,header ,level))
2172
2173 (defun gnus-data-enter (after-article number mark pos header level offset)
2174   (let ((data (gnus-data-find-list after-article)))
2175     (unless data
2176       (error "No such article: %d" after-article))
2177     (setcdr data (cons (gnus-data-make number mark pos header level)
2178                        (cdr data)))
2179     (setq gnus-newsgroup-data-reverse nil)
2180     (gnus-data-update-list (cddr data) offset)))
2181
2182 (defun gnus-data-enter-list (after-article list &optional offset)
2183   (when list
2184     (let ((data (and after-article (gnus-data-find-list after-article)))
2185           (ilist list))
2186       (if (not (or data
2187                    after-article))
2188           (let ((odata gnus-newsgroup-data))
2189             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2190             (when offset
2191               (gnus-data-update-list odata offset)))
2192         ;; Find the last element in the list to be spliced into the main
2193         ;; list.
2194         (while (cdr list)
2195           (setq list (cdr list)))
2196         (if (not data)
2197             (progn
2198               (setcdr list gnus-newsgroup-data)
2199               (setq gnus-newsgroup-data ilist)
2200               (when offset
2201                 (gnus-data-update-list (cdr list) offset)))
2202           (setcdr list (cdr data))
2203           (setcdr data ilist)
2204           (when offset
2205             (gnus-data-update-list (cdr list) offset))))
2206       (setq gnus-newsgroup-data-reverse nil))))
2207
2208 (defun gnus-data-remove (article &optional offset)
2209   (let ((data gnus-newsgroup-data))
2210     (if (= (gnus-data-number (car data)) article)
2211         (progn
2212           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2213                 gnus-newsgroup-data-reverse nil)
2214           (when offset
2215             (gnus-data-update-list gnus-newsgroup-data offset)))
2216       (while (cdr data)
2217         (when (= (gnus-data-number (cadr data)) article)
2218           (setcdr data (cddr data))
2219           (when offset
2220             (gnus-data-update-list (cdr data) offset))
2221           (setq data nil
2222                 gnus-newsgroup-data-reverse nil))
2223         (setq data (cdr data))))))
2224
2225 (defmacro gnus-data-list (backward)
2226   `(if ,backward
2227        (or gnus-newsgroup-data-reverse
2228            (setq gnus-newsgroup-data-reverse
2229                  (reverse gnus-newsgroup-data)))
2230      gnus-newsgroup-data))
2231
2232 (defun gnus-data-update-list (data offset)
2233   "Add OFFSET to the POS of all data entries in DATA."
2234   (setq gnus-newsgroup-data-reverse nil)
2235   (while data
2236     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2237     (setq data (cdr data))))
2238
2239 (defun gnus-summary-article-pseudo-p (article)
2240   "Say whether this article is a pseudo article or not."
2241   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2242
2243 (defmacro gnus-summary-article-sparse-p (article)
2244   "Say whether this article is a sparse article or not."
2245   `(memq ,article gnus-newsgroup-sparse))
2246
2247 (defmacro gnus-summary-article-ancient-p (article)
2248   "Say whether this article is a sparse article or not."
2249   `(memq ,article gnus-newsgroup-ancient))
2250
2251 (defun gnus-article-parent-p (number)
2252   "Say whether this article is a parent or not."
2253   (let ((data (gnus-data-find-list number)))
2254     (and (cdr data)                     ; There has to be an article after...
2255          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2256             (gnus-data-level (nth 1 data))))))
2257
2258 (defun gnus-article-children (number)
2259   "Return a list of all children to NUMBER."
2260   (let* ((data (gnus-data-find-list number))
2261          (level (gnus-data-level (car data)))
2262          children)
2263     (setq data (cdr data))
2264     (while (and data
2265                 (= (gnus-data-level (car data)) (1+ level)))
2266       (push (gnus-data-number (car data)) children)
2267       (setq data (cdr data)))
2268     children))
2269
2270 (defmacro gnus-summary-skip-intangible ()
2271   "If the current article is intangible, then jump to a different article."
2272   '(let ((to (get-text-property (point) 'gnus-intangible)))
2273      (and to (gnus-summary-goto-subject to))))
2274
2275 (defmacro gnus-summary-article-intangible-p ()
2276   "Say whether this article is intangible or not."
2277   '(get-text-property (point) 'gnus-intangible))
2278
2279 (defun gnus-article-read-p (article)
2280   "Say whether ARTICLE is read or not."
2281   (not (or (memq article gnus-newsgroup-marked)
2282            (memq article gnus-newsgroup-unreads)
2283            (memq article gnus-newsgroup-unselected)
2284            (memq article gnus-newsgroup-dormant))))
2285
2286 ;; Some summary mode macros.
2287
2288 (defmacro gnus-summary-article-number ()
2289   "The article number of the article on the current line.
2290 If there isn's an article number here, then we return the current
2291 article number."
2292   '(progn
2293      (gnus-summary-skip-intangible)
2294      (or (get-text-property (point) 'gnus-number)
2295          (gnus-summary-last-subject))))
2296
2297 (defmacro gnus-summary-article-header (&optional number)
2298   "Return the header of article NUMBER."
2299   `(gnus-data-header (gnus-data-find
2300                       ,(or number '(gnus-summary-article-number)))))
2301
2302 (defmacro gnus-summary-thread-level (&optional number)
2303   "Return the level of thread that starts with article NUMBER."
2304   `(if (and (eq gnus-summary-make-false-root 'dummy)
2305             (get-text-property (point) 'gnus-intangible))
2306        0
2307      (gnus-data-level (gnus-data-find
2308                        ,(or number '(gnus-summary-article-number))))))
2309
2310 (defmacro gnus-summary-article-mark (&optional number)
2311   "Return the mark of article NUMBER."
2312   `(gnus-data-mark (gnus-data-find
2313                     ,(or number '(gnus-summary-article-number)))))
2314
2315 (defmacro gnus-summary-article-pos (&optional number)
2316   "Return the position of the line of article NUMBER."
2317   `(gnus-data-pos (gnus-data-find
2318                    ,(or number '(gnus-summary-article-number)))))
2319
2320 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2321 (defmacro gnus-summary-article-subject (&optional number)
2322   "Return current subject string or nil if nothing."
2323   `(let ((headers
2324           ,(if number
2325                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2326              '(gnus-data-header (assq (gnus-summary-article-number)
2327                                       gnus-newsgroup-data)))))
2328      (and headers
2329           (vectorp headers)
2330           (mail-header-subject headers))))
2331
2332 (defmacro gnus-summary-article-score (&optional number)
2333   "Return current article score."
2334   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2335                   gnus-newsgroup-scored))
2336        gnus-summary-default-score 0))
2337
2338 (defun gnus-summary-article-children (&optional number)
2339   "Return a list of article numbers that are children of article NUMBER."
2340   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2341          (level (gnus-data-level (car data)))
2342          l children)
2343     (while (and (setq data (cdr data))
2344                 (> (setq l (gnus-data-level (car data))) level))
2345       (and (= (1+ level) l)
2346            (push (gnus-data-number (car data))
2347                  children)))
2348     (nreverse children)))
2349
2350 (defun gnus-summary-article-parent (&optional number)
2351   "Return the article number of the parent of article NUMBER."
2352   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2353                                     (gnus-data-list t)))
2354          (level (gnus-data-level (car data))))
2355     (if (zerop level)
2356         ()                              ; This is a root.
2357       ;; We search until we find an article with a level less than
2358       ;; this one.  That function has to be the parent.
2359       (while (and (setq data (cdr data))
2360                   (not (< (gnus-data-level (car data)) level))))
2361       (and data (gnus-data-number (car data))))))
2362
2363 (defun gnus-unread-mark-p (mark)
2364   "Say whether MARK is the unread mark."
2365   (= mark gnus-unread-mark))
2366
2367 (defun gnus-read-mark-p (mark)
2368   "Say whether MARK is one of the marks that mark as read.
2369 This is all marks except unread, ticked, dormant, and expirable."
2370   (not (or (= mark gnus-unread-mark)
2371            (= mark gnus-ticked-mark)
2372            (= mark gnus-dormant-mark)
2373            (= mark gnus-expirable-mark))))
2374
2375 (defmacro gnus-article-mark (number)
2376   "Return the MARK of article NUMBER.
2377 This macro should only be used when computing the mark the \"first\"
2378 time; i.e., when generating the summary lines.  After that,
2379 `gnus-summary-article-mark' should be used to examine the
2380 marks of articles."
2381   `(cond
2382     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2383     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2384     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2385     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2386     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2387     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2388     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2389     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2390            gnus-ancient-mark))))
2391
2392 ;; Saving hidden threads.
2393
2394 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2395 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2396
2397 (defmacro gnus-save-hidden-threads (&rest forms)
2398   "Save hidden threads, eval FORMS, and restore the hidden threads."
2399   (let ((config (make-symbol "config")))
2400     `(let ((,config (gnus-hidden-threads-configuration)))
2401        (unwind-protect
2402            (save-excursion
2403              ,@forms)
2404          (gnus-restore-hidden-threads-configuration ,config)))))
2405
2406 (defun gnus-data-compute-positions ()
2407   "Compute the positions of all articles."
2408   (setq gnus-newsgroup-data-reverse nil)
2409   (let ((data gnus-newsgroup-data))
2410     (save-excursion
2411       (gnus-save-hidden-threads
2412         (gnus-summary-show-all-threads)
2413         (goto-char (point-min))
2414         (while data
2415           (while (get-text-property (point) 'gnus-intangible)
2416             (forward-line 1))
2417           (gnus-data-set-pos (car data) (+ (point) 3))
2418           (setq data (cdr data))
2419           (forward-line 1))))))
2420
2421 (defun gnus-hidden-threads-configuration ()
2422   "Return the current hidden threads configuration."
2423   (save-excursion
2424     (let (config)
2425       (goto-char (point-min))
2426       (while (search-forward "\r" nil t)
2427         (push (1- (point)) config))
2428       config)))
2429
2430 (defun gnus-restore-hidden-threads-configuration (config)
2431   "Restore hidden threads configuration from CONFIG."
2432   (save-excursion
2433     (let (point buffer-read-only)
2434       (while (setq point (pop config))
2435         (when (and (< point (point-max))
2436                    (goto-char point)
2437                    (eq (char-after) ?\n))
2438           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2439
2440 ;; Various summary mode internalish functions.
2441
2442 (defun gnus-mouse-pick-article (e)
2443   (interactive "e")
2444   (mouse-set-point e)
2445   (gnus-summary-next-page nil t))
2446
2447 (defun gnus-summary-set-display-table ()
2448   "Change the display table.
2449 Odd characters have a tendency to mess
2450 up nicely formatted displays - we make all possible glyphs
2451 display only a single character."
2452
2453   ;; We start from the standard display table, if any.
2454   (let ((table (or (copy-sequence standard-display-table)
2455                    (make-display-table)))
2456         (i 32))
2457     ;; Nix out all the control chars...
2458     (while (>= (setq i (1- i)) 0)
2459       (aset table i [??]))
2460     ;; ... but not newline and cr, of course.  (cr is necessary for the
2461     ;; selective display).
2462     (aset table ?\n nil)
2463     (aset table ?\r nil)
2464     ;; We keep TAB as well.
2465     (aset table ?\t nil)
2466     ;; We nix out any glyphs over 126 that are not set already.
2467     (let ((i 256))
2468       (while (>= (setq i (1- i)) 127)
2469         ;; Only modify if the entry is nil.
2470         (unless (aref table i)
2471           (aset table i [??]))))
2472     (setq buffer-display-table table)))
2473
2474 (defun gnus-summary-setup-buffer (group)
2475   "Initialize summary buffer."
2476   (let ((buffer (concat "*Summary " group "*")))
2477     (if (get-buffer buffer)
2478         (progn
2479           (set-buffer buffer)
2480           (setq gnus-summary-buffer (current-buffer))
2481           (not gnus-newsgroup-prepared))
2482       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2483       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2484       (gnus-summary-mode group)
2485       (when gnus-carpal
2486         (gnus-carpal-setup-buffer 'summary))
2487       (unless gnus-single-article-buffer
2488         (make-local-variable 'gnus-article-buffer)
2489         (make-local-variable 'gnus-article-current)
2490         (make-local-variable 'gnus-original-article-buffer))
2491       (setq gnus-newsgroup-name group)
2492       t)))
2493
2494 (defun gnus-set-global-variables ()
2495   "Set the global equivalents of the buffer-local variables.
2496 They are set to the latest values they had.  These reflect the summary
2497 buffer that was in action when the last article was fetched."
2498   (when (eq major-mode 'gnus-summary-mode)
2499     (setq gnus-summary-buffer (current-buffer))
2500     (let ((name gnus-newsgroup-name)
2501           (marked gnus-newsgroup-marked)
2502           (unread gnus-newsgroup-unreads)
2503           (headers gnus-current-headers)
2504           (data gnus-newsgroup-data)
2505           (summary gnus-summary-buffer)
2506           (article-buffer gnus-article-buffer)
2507           (original gnus-original-article-buffer)
2508           (gac gnus-article-current)
2509           (reffed gnus-reffed-article-number)
2510           (score-file gnus-current-score-file)
2511           (default-charset gnus-newsgroup-charset))
2512       (save-excursion
2513         (set-buffer gnus-group-buffer)
2514         (setq gnus-newsgroup-name name
2515               gnus-newsgroup-marked marked
2516               gnus-newsgroup-unreads unread
2517               gnus-current-headers headers
2518               gnus-newsgroup-data data
2519               gnus-article-current gac
2520               gnus-summary-buffer summary
2521               gnus-article-buffer article-buffer
2522               gnus-original-article-buffer original
2523               gnus-reffed-article-number reffed
2524               gnus-current-score-file score-file
2525               gnus-newsgroup-charset default-charset)
2526         ;; The article buffer also has local variables.
2527         (when (gnus-buffer-live-p gnus-article-buffer)
2528           (set-buffer gnus-article-buffer)
2529           (setq gnus-summary-buffer summary))))))
2530
2531 (defun gnus-summary-article-unread-p (article)
2532   "Say whether ARTICLE is unread or not."
2533   (memq article gnus-newsgroup-unreads))
2534
2535 (defun gnus-summary-first-article-p (&optional article)
2536   "Return whether ARTICLE is the first article in the buffer."
2537   (if (not (setq article (or article (gnus-summary-article-number))))
2538       nil
2539     (eq article (caar gnus-newsgroup-data))))
2540
2541 (defun gnus-summary-last-article-p (&optional article)
2542   "Return whether ARTICLE is the last article in the buffer."
2543   (if (not (setq article (or article (gnus-summary-article-number))))
2544       ;; All non-existent numbers are the last article.  :-)
2545       t
2546     (not (cdr (gnus-data-find-list article)))))
2547
2548 (defun gnus-make-thread-indent-array ()
2549   (let ((n 200))
2550     (unless (and gnus-thread-indent-array
2551                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2552       (setq gnus-thread-indent-array (make-vector 201 "")
2553             gnus-thread-indent-array-level gnus-thread-indent-level)
2554       (while (>= n 0)
2555         (aset gnus-thread-indent-array n
2556               (make-string (* n gnus-thread-indent-level) ? ))
2557         (setq n (1- n))))))
2558
2559 (defun gnus-update-summary-mark-positions ()
2560   "Compute where the summary marks are to go."
2561   (save-excursion
2562     (when (gnus-buffer-exists-p gnus-summary-buffer)
2563       (set-buffer gnus-summary-buffer))
2564     (let ((gnus-replied-mark 129)
2565           (gnus-score-below-mark 130)
2566           (gnus-score-over-mark 130)
2567           (gnus-download-mark 131)
2568           (spec gnus-summary-line-format-spec)
2569           gnus-visual pos)
2570       (save-excursion
2571         (gnus-set-work-buffer)
2572         (let ((gnus-summary-line-format-spec spec)
2573               (gnus-newsgroup-downloadable '((0 . t))))
2574           (gnus-summary-insert-line
2575            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2576           (goto-char (point-min))
2577           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2578                                              (- (point) 2)))))
2579           (goto-char (point-min))
2580           (push (cons 'replied (and (search-forward "\201" nil t)
2581                                     (- (point) 2)))
2582                 pos)
2583           (goto-char (point-min))
2584           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2585                 pos)
2586           (goto-char (point-min))
2587           (push (cons 'download
2588                       (and (search-forward "\203" nil t) (- (point) 2)))
2589                 pos)))
2590       (setq gnus-summary-mark-positions pos))))
2591
2592 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2593   "Insert a dummy root in the summary buffer."
2594   (beginning-of-line)
2595   (gnus-add-text-properties
2596    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2597    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2598
2599 (defun gnus-summary-from-or-to-or-newsgroups (header)
2600   (let ((to (cdr (assq 'To (mail-header-extra header))))
2601         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2602         (mail-parse-charset gnus-newsgroup-charset)
2603         (mail-parse-ignored-charsets
2604          (save-excursion (set-buffer gnus-summary-buffer)
2605                          gnus-newsgroup-ignored-charsets)))
2606     (cond
2607      ((and to
2608            gnus-ignored-from-addresses
2609            (string-match gnus-ignored-from-addresses
2610                          (mail-header-from header)))
2611       (concat "-> "
2612               (or (car (funcall gnus-extract-address-components
2613                                 (funcall
2614                                  gnus-decode-encoded-word-function to)))
2615                   (funcall gnus-decode-encoded-word-function to))))
2616      ((and newsgroups
2617            gnus-ignored-from-addresses
2618            (string-match gnus-ignored-from-addresses
2619                          (mail-header-from header)))
2620       (concat "=> " newsgroups))
2621      (t
2622       (or (car (funcall gnus-extract-address-components
2623                         (mail-header-from header)))
2624           (mail-header-from header))))))
2625
2626 (defun gnus-summary-insert-line (gnus-tmp-header
2627                                  gnus-tmp-level gnus-tmp-current
2628                                  gnus-tmp-unread gnus-tmp-replied
2629                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2630                                  &optional gnus-tmp-dummy gnus-tmp-score
2631                                  gnus-tmp-process)
2632   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2633          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2634          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2635          (gnus-tmp-score-char
2636           (if (or (null gnus-summary-default-score)
2637                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2638                       gnus-summary-zcore-fuzz))
2639               ?  ;Whitespace
2640             (if (< gnus-tmp-score gnus-summary-default-score)
2641                 gnus-score-below-mark gnus-score-over-mark)))
2642          (gnus-tmp-replied
2643           (cond (gnus-tmp-process gnus-process-mark)
2644                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2645                  gnus-cached-mark)
2646                 (gnus-tmp-replied gnus-replied-mark)
2647                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2648                  gnus-saved-mark)
2649                 (t gnus-unread-mark)))
2650          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2651          (gnus-tmp-name
2652           (cond
2653            ((string-match "<[^>]+> *$" gnus-tmp-from)
2654             (let ((beg (match-beginning 0)))
2655               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2656                        (substring gnus-tmp-from (1+ (match-beginning 0))
2657                                   (1- (match-end 0))))
2658                   (substring gnus-tmp-from 0 beg))))
2659            ((string-match "(.+)" gnus-tmp-from)
2660             (substring gnus-tmp-from
2661                        (1+ (match-beginning 0)) (1- (match-end 0))))
2662            (t gnus-tmp-from)))
2663          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2664          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2665          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2666          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2667          (buffer-read-only nil))
2668     (when (string= gnus-tmp-name "")
2669       (setq gnus-tmp-name gnus-tmp-from))
2670     (unless (numberp gnus-tmp-lines)
2671       (setq gnus-tmp-lines 0))
2672     (gnus-put-text-property
2673      (point)
2674      (progn (eval gnus-summary-line-format-spec) (point))
2675      'gnus-number gnus-tmp-number)
2676     (when (gnus-visual-p 'summary-highlight 'highlight)
2677       (forward-line -1)
2678       (gnus-run-hooks 'gnus-summary-update-hook)
2679       (forward-line 1))))
2680
2681 (defun gnus-summary-update-line (&optional dont-update)
2682   "Update summary line after change."
2683   (when (and gnus-summary-default-score
2684              (not gnus-summary-inhibit-highlight))
2685     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2686            (article (gnus-summary-article-number))
2687            (score (gnus-summary-article-score article)))
2688       (unless dont-update
2689         (if (and gnus-summary-mark-below
2690                  (< (gnus-summary-article-score)
2691                     gnus-summary-mark-below))
2692             ;; This article has a low score, so we mark it as read.
2693             (when (memq article gnus-newsgroup-unreads)
2694               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2695           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2696             ;; This article was previously marked as read on account
2697             ;; of a low score, but now it has risen, so we mark it as
2698             ;; unread.
2699             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2700         (gnus-summary-update-mark
2701          (if (or (null gnus-summary-default-score)
2702                  (<= (abs (- score gnus-summary-default-score))
2703                      gnus-summary-zcore-fuzz))
2704              ?  ;Whitespace
2705            (if (< score gnus-summary-default-score)
2706                gnus-score-below-mark gnus-score-over-mark))
2707          'score))
2708       ;; Do visual highlighting.
2709       (when (gnus-visual-p 'summary-highlight 'highlight)
2710         (gnus-run-hooks 'gnus-summary-update-hook)))))
2711
2712 (defvar gnus-tmp-new-adopts nil)
2713
2714 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2715   "Return the number of articles in THREAD.
2716 This may be 0 in some cases -- if none of the articles in
2717 the thread are to be displayed."
2718   (let* ((number
2719           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2720           (cond
2721            ((not (listp thread))
2722             1)
2723            ((and (consp thread) (cdr thread))
2724             (apply
2725              '+ 1 (mapcar
2726                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2727            ((null thread)
2728             1)
2729            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2730             1)
2731            (t 0))))
2732     (when (and level (zerop level) gnus-tmp-new-adopts)
2733       (incf number
2734             (apply '+ (mapcar
2735                        'gnus-summary-number-of-articles-in-thread
2736                        gnus-tmp-new-adopts))))
2737     (if char
2738         (if (> number 1) gnus-not-empty-thread-mark
2739           gnus-empty-thread-mark)
2740       number)))
2741
2742 (defun gnus-summary-set-local-parameters (group)
2743   "Go through the local params of GROUP and set all variable specs in that list."
2744   (let ((params (gnus-group-find-parameter group))
2745         elem)
2746     (while params
2747       (setq elem (car params)
2748             params (cdr params))
2749       (and (consp elem)                 ; Has to be a cons.
2750            (consp (cdr elem))           ; The cdr has to be a list.
2751            (symbolp (car elem))         ; Has to be a symbol in there.
2752            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2753            (ignore-errors               ; So we set it.
2754              (make-local-variable (car elem))
2755              (set (car elem) (eval (nth 1 elem))))))))
2756
2757 (defun gnus-summary-read-group (group &optional show-all no-article
2758                                       kill-buffer no-display backward
2759                                       select-articles)
2760   "Start reading news in newsgroup GROUP.
2761 If SHOW-ALL is non-nil, already read articles are also listed.
2762 If NO-ARTICLE is non-nil, no article is selected initially.
2763 If NO-DISPLAY, don't generate a summary buffer."
2764   (let (result)
2765     (while (and group
2766                 (null (setq result
2767                             (let ((gnus-auto-select-next nil))
2768                               (or (gnus-summary-read-group-1
2769                                    group show-all no-article
2770                                    kill-buffer no-display
2771                                    select-articles)
2772                                   (setq show-all nil
2773                                         select-articles nil)))))
2774                 (eq gnus-auto-select-next 'quietly))
2775       (set-buffer gnus-group-buffer)
2776       ;; The entry function called above goes to the next
2777       ;; group automatically, so we go two groups back
2778       ;; if we are searching for the previous group.
2779       (when backward
2780         (gnus-group-prev-unread-group 2))
2781       (if (not (equal group (gnus-group-group-name)))
2782           (setq group (gnus-group-group-name))
2783         (setq group nil)))
2784     result))
2785
2786 (defun gnus-summary-read-group-1 (group show-all no-article
2787                                         kill-buffer no-display
2788                                         &optional select-articles)
2789   ;; Killed foreign groups can't be entered.
2790   (when (and (not (gnus-group-native-p group))
2791              (not (gnus-gethash group gnus-newsrc-hashtb)))
2792     (error "Dead non-native groups can't be entered"))
2793   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2794   (let* ((new-group (gnus-summary-setup-buffer group))
2795          (quit-config (gnus-group-quit-config group))
2796          (did-select (and new-group (gnus-select-newsgroup
2797                                      group show-all select-articles))))
2798     (cond
2799      ;; This summary buffer exists already, so we just select it.
2800      ((not new-group)
2801       (gnus-set-global-variables)
2802       (when kill-buffer
2803         (gnus-kill-or-deaden-summary kill-buffer))
2804       (gnus-configure-windows 'summary 'force)
2805       (gnus-set-mode-line 'summary)
2806       (gnus-summary-position-point)
2807       (message "")
2808       t)
2809      ;; We couldn't select this group.
2810      ((null did-select)
2811       (when (and (eq major-mode 'gnus-summary-mode)
2812                  (not (equal (current-buffer) kill-buffer)))
2813         (kill-buffer (current-buffer))
2814         (if (not quit-config)
2815             (progn
2816               ;; Update the info -- marks might need to be removed,
2817               ;; for instance.
2818               (gnus-summary-update-info)
2819               (set-buffer gnus-group-buffer)
2820               (gnus-group-jump-to-group group)
2821               (gnus-group-next-unread-group 1))
2822           (gnus-handle-ephemeral-exit quit-config)))
2823       (gnus-message 3 "Can't select group")
2824       nil)
2825      ;; The user did a `C-g' while prompting for number of articles,
2826      ;; so we exit this group.
2827      ((eq did-select 'quit)
2828       (and (eq major-mode 'gnus-summary-mode)
2829            (not (equal (current-buffer) kill-buffer))
2830            (kill-buffer (current-buffer)))
2831       (when kill-buffer
2832         (gnus-kill-or-deaden-summary kill-buffer))
2833       (if (not quit-config)
2834           (progn
2835             (set-buffer gnus-group-buffer)
2836             (gnus-group-jump-to-group group)
2837             (gnus-group-next-unread-group 1)
2838             (gnus-configure-windows 'group 'force))
2839         (gnus-handle-ephemeral-exit quit-config))
2840       ;; Finally signal the quit.
2841       (signal 'quit nil))
2842      ;; The group was successfully selected.
2843      (t
2844       (gnus-set-global-variables)
2845       ;; Save the active value in effect when the group was entered.
2846       (setq gnus-newsgroup-active
2847             (gnus-copy-sequence
2848              (gnus-active gnus-newsgroup-name)))
2849       ;; You can change the summary buffer in some way with this hook.
2850       (gnus-run-hooks 'gnus-select-group-hook)
2851       ;; Set any local variables in the group parameters.
2852       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2853       (gnus-update-format-specifications
2854        nil 'summary 'summary-mode 'summary-dummy)
2855       (gnus-update-summary-mark-positions)
2856       ;; Do score processing.
2857       (when gnus-use-scoring
2858         (gnus-possibly-score-headers))
2859       ;; Check whether to fill in the gaps in the threads.
2860       (when gnus-build-sparse-threads
2861         (gnus-build-sparse-threads))
2862       ;; Find the initial limit.
2863       (if gnus-show-threads
2864           (if show-all
2865               (let ((gnus-newsgroup-dormant nil))
2866                 (gnus-summary-initial-limit show-all))
2867             (gnus-summary-initial-limit show-all))
2868         ;; When untreaded, all articles are always shown.
2869         (setq gnus-newsgroup-limit
2870               (mapcar
2871                (lambda (header) (mail-header-number header))
2872                gnus-newsgroup-headers)))
2873       ;; Generate the summary buffer.
2874       (unless no-display
2875         (gnus-summary-prepare))
2876       (when gnus-use-trees
2877         (gnus-tree-open group)
2878         (setq gnus-summary-highlight-line-function
2879               'gnus-tree-highlight-article))
2880       ;; If the summary buffer is empty, but there are some low-scored
2881       ;; articles or some excluded dormants, we include these in the
2882       ;; buffer.
2883       (when (and (zerop (buffer-size))
2884                  (not no-display))
2885         (cond (gnus-newsgroup-dormant
2886                (gnus-summary-limit-include-dormant))
2887               ((and gnus-newsgroup-scored show-all)
2888                (gnus-summary-limit-include-expunged t))))
2889       ;; Function `gnus-apply-kill-file' must be called in this hook.
2890       (gnus-run-hooks 'gnus-apply-kill-hook)
2891       (if (and (zerop (buffer-size))
2892                (not no-display))
2893           (progn
2894             ;; This newsgroup is empty.
2895             (gnus-summary-catchup-and-exit nil t)
2896             (gnus-message 6 "No unread news")
2897             (when kill-buffer
2898               (gnus-kill-or-deaden-summary kill-buffer))
2899             ;; Return nil from this function.
2900             nil)
2901         ;; Hide conversation thread subtrees.  We cannot do this in
2902         ;; gnus-summary-prepare-hook since kill processing may not
2903         ;; work with hidden articles.
2904         (and gnus-show-threads
2905              gnus-thread-hide-subtree
2906              (gnus-summary-hide-all-threads))
2907         (when kill-buffer
2908           (gnus-kill-or-deaden-summary kill-buffer))
2909         ;; Show first unread article if requested.
2910         (if (and (not no-article)
2911                  (not no-display)
2912                  gnus-newsgroup-unreads
2913                  gnus-auto-select-first)
2914             (progn
2915               (gnus-configure-windows 'summary)
2916               (cond
2917                ((eq gnus-auto-select-first 'best)
2918                 (gnus-summary-best-unread-article))
2919                ((eq gnus-auto-select-first t)
2920                 (gnus-summary-first-unread-article))
2921                ((gnus-functionp gnus-auto-select-first)
2922                 (funcall gnus-auto-select-first))))
2923           ;; Don't select any articles, just move point to the first
2924           ;; article in the group.
2925           (goto-char (point-min))
2926           (gnus-summary-position-point)
2927           (gnus-configure-windows 'summary 'force)
2928           (gnus-set-mode-line 'summary))
2929         (when (get-buffer-window gnus-group-buffer t)
2930           ;; Gotta use windows, because recenter does weird stuff if
2931           ;; the current buffer ain't the displayed window.
2932           (let ((owin (selected-window)))
2933             (select-window (get-buffer-window gnus-group-buffer t))
2934             (when (gnus-group-goto-group group)
2935               (recenter))
2936             (select-window owin)))
2937         ;; Mark this buffer as "prepared".
2938         (setq gnus-newsgroup-prepared t)
2939         (gnus-run-hooks 'gnus-summary-prepared-hook)
2940         t)))))
2941
2942 (defun gnus-summary-prepare ()
2943   "Generate the summary buffer."
2944   (interactive)
2945   (let ((buffer-read-only nil))
2946     (erase-buffer)
2947     (setq gnus-newsgroup-data nil
2948           gnus-newsgroup-data-reverse nil)
2949     (gnus-run-hooks 'gnus-summary-generate-hook)
2950     ;; Generate the buffer, either with threads or without.
2951     (when gnus-newsgroup-headers
2952       (gnus-summary-prepare-threads
2953        (if gnus-show-threads
2954            (gnus-sort-gathered-threads
2955             (funcall gnus-summary-thread-gathering-function
2956                      (gnus-sort-threads
2957                       (gnus-cut-threads (gnus-make-threads)))))
2958          ;; Unthreaded display.
2959          (gnus-sort-articles gnus-newsgroup-headers))))
2960     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2961     ;; Call hooks for modifying summary buffer.
2962     (goto-char (point-min))
2963     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2964
2965 (defsubst gnus-general-simplify-subject (subject)
2966   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2967   (setq subject
2968         (cond
2969          ;; Truncate the subject.
2970          (gnus-simplify-subject-functions
2971           (gnus-map-function gnus-simplify-subject-functions subject))
2972          ((numberp gnus-summary-gather-subject-limit)
2973           (setq subject (gnus-simplify-subject-re subject))
2974           (if (> (length subject) gnus-summary-gather-subject-limit)
2975               (substring subject 0 gnus-summary-gather-subject-limit)
2976             subject))
2977          ;; Fuzzily simplify it.
2978          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2979           (gnus-simplify-subject-fuzzy subject))
2980          ;; Just remove the leading "Re:".
2981          (t
2982           (gnus-simplify-subject-re subject))))
2983
2984   (if (and gnus-summary-gather-exclude-subject
2985            (string-match gnus-summary-gather-exclude-subject subject))
2986       nil                               ; This article shouldn't be gathered
2987     subject))
2988
2989 (defun gnus-summary-simplify-subject-query ()
2990   "Query where the respool algorithm would put this article."
2991   (interactive)
2992   (gnus-summary-select-article)
2993   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2994
2995 (defun gnus-gather-threads-by-subject (threads)
2996   "Gather threads by looking at Subject headers."
2997   (if (not gnus-summary-make-false-root)
2998       threads
2999     (let ((hashtb (gnus-make-hashtable 1024))
3000           (prev threads)
3001           (result threads)
3002           subject hthread whole-subject)
3003       (while threads
3004         (setq subject (gnus-general-simplify-subject
3005                        (setq whole-subject (mail-header-subject
3006                                             (caar threads)))))
3007         (when subject
3008           (if (setq hthread (gnus-gethash subject hashtb))
3009               (progn
3010                 ;; We enter a dummy root into the thread, if we
3011                 ;; haven't done that already.
3012                 (unless (stringp (caar hthread))
3013                   (setcar hthread (list whole-subject (car hthread))))
3014                 ;; We add this new gathered thread to this gathered
3015                 ;; thread.
3016                 (setcdr (car hthread)
3017                         (nconc (cdar hthread) (list (car threads))))
3018                 ;; Remove it from the list of threads.
3019                 (setcdr prev (cdr threads))
3020                 (setq threads prev))
3021             ;; Enter this thread into the hash table.
3022             (gnus-sethash subject threads hashtb)))
3023         (setq prev threads)
3024         (setq threads (cdr threads)))
3025       result)))
3026
3027 (defun gnus-gather-threads-by-references (threads)
3028   "Gather threads by looking at References headers."
3029   (let ((idhashtb (gnus-make-hashtable 1024))
3030         (thhashtb (gnus-make-hashtable 1024))
3031         (prev threads)
3032         (result threads)
3033         ids references id gthread gid entered ref)
3034     (while threads
3035       (when (setq references (mail-header-references (caar threads)))
3036         (setq id (mail-header-id (caar threads))
3037               ids (gnus-split-references references)
3038               entered nil)
3039         (while (setq ref (pop ids))
3040           (setq ids (delete ref ids))
3041           (if (not (setq gid (gnus-gethash ref idhashtb)))
3042               (progn
3043                 (gnus-sethash ref id idhashtb)
3044                 (gnus-sethash id threads thhashtb))
3045             (setq gthread (gnus-gethash gid thhashtb))
3046             (unless entered
3047               ;; We enter a dummy root into the thread, if we
3048               ;; haven't done that already.
3049               (unless (stringp (caar gthread))
3050                 (setcar gthread (list (mail-header-subject (caar gthread))
3051                                       (car gthread))))
3052               ;; We add this new gathered thread to this gathered
3053               ;; thread.
3054               (setcdr (car gthread)
3055                       (nconc (cdar gthread) (list (car threads)))))
3056             ;; Add it into the thread hash table.
3057             (gnus-sethash id gthread thhashtb)
3058             (setq entered t)
3059             ;; Remove it from the list of threads.
3060             (setcdr prev (cdr threads))
3061             (setq threads prev))))
3062       (setq prev threads)
3063       (setq threads (cdr threads)))
3064     result))
3065
3066 (defun gnus-sort-gathered-threads (threads)
3067   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3068   (let ((result threads))
3069     (while threads
3070       (when (stringp (caar threads))
3071         (setcdr (car threads)
3072                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3073       (setq threads (cdr threads)))
3074     result))
3075
3076 (defun gnus-thread-loop-p (root thread)
3077   "Say whether ROOT is in THREAD."
3078   (let ((stack (list thread))
3079         (infloop 0)
3080         th)
3081     (while (setq thread (pop stack))
3082       (setq th (cdr thread))
3083       (while (and th
3084                   (not (eq (caar th) root)))
3085         (pop th))
3086       (if th
3087           ;; We have found a loop.
3088           (let (ref-dep)
3089             (setcdr thread (delq (car th) (cdr thread)))
3090             (if (boundp (setq ref-dep (intern "none"
3091                                               gnus-newsgroup-dependencies)))
3092                 (setcdr (symbol-value ref-dep)
3093                         (nconc (cdr (symbol-value ref-dep))
3094                                (list (car th))))
3095               (set ref-dep (list nil (car th))))
3096             (setq infloop 1
3097                   stack nil))
3098         ;; Push all the subthreads onto the stack.
3099         (push (cdr thread) stack)))
3100     infloop))
3101
3102 (defun gnus-make-threads ()
3103   "Go through the dependency hashtb and find the roots.  Return all threads."
3104   (let (threads)
3105     (while (catch 'infloop
3106              (mapatoms
3107               (lambda (refs)
3108                 ;; Deal with self-referencing References loops.
3109                 (when (and (car (symbol-value refs))
3110                            (not (zerop
3111                                  (apply
3112                                   '+
3113                                   (mapcar
3114                                    (lambda (thread)
3115                                      (gnus-thread-loop-p
3116                                       (car (symbol-value refs)) thread))
3117                                    (cdr (symbol-value refs)))))))
3118                   (setq threads nil)
3119                   (throw 'infloop t))
3120                 (unless (car (symbol-value refs))
3121                   ;; These threads do not refer back to any other articles,
3122                   ;; so they're roots.
3123                   (setq threads (append (cdr (symbol-value refs)) threads))))
3124               gnus-newsgroup-dependencies)))
3125     threads))
3126
3127 ;; Build the thread tree.
3128 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3129   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3130
3131 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3132 if it was already present.
3133
3134 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3135 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3136 Message-IDs will be renamed be renamed to a unique Message-ID before
3137 being entered.
3138
3139 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3140   (let* ((id (mail-header-id header))
3141          (id-dep (and id (intern id dependencies)))
3142          ref ref-dep ref-header)
3143     ;; Enter this `header' in the `dependencies' table.
3144     (cond
3145      ((not id-dep)
3146       (setq header nil))
3147      ;; The first two cases do the normal part: enter a new `header'
3148      ;; in the `dependencies' table.
3149      ((not (boundp id-dep))
3150       (set id-dep (list header)))
3151      ((null (car (symbol-value id-dep)))
3152       (setcar (symbol-value id-dep) header))
3153
3154      ;; From here the `header' was already present in the
3155      ;; `dependencies' table.
3156      (force-new
3157       ;; Overrides an existing entry;
3158       ;; just set the header part of the entry.
3159       (setcar (symbol-value id-dep) header))
3160
3161      ;; Renames the existing `header' to a unique Message-ID.
3162      ((not gnus-summary-ignore-duplicates)
3163       ;; An article with this Message-ID has already been seen.
3164       ;; We rename the Message-ID.
3165       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3166            (list header))
3167       (mail-header-set-id header id))
3168
3169      ;; The last case ignores an existing entry, except it adds any
3170      ;; additional Xrefs (in case the two articles came from different
3171      ;; servers.
3172      ;; Also sets `header' to `nil' meaning that the `dependencies'
3173      ;; table was *not* modified.
3174      (t
3175       (mail-header-set-xref
3176        (car (symbol-value id-dep))
3177        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3178                    "")
3179                (or (mail-header-xref header) "")))
3180       (setq header nil)))
3181
3182     (when header
3183       ;; First check if that we are not creating a References loop.
3184       (setq ref (gnus-parent-id (mail-header-references header)))
3185       (while (and ref
3186                   (setq ref-dep (intern-soft ref dependencies))
3187                   (boundp ref-dep)
3188                   (setq ref-header (car (symbol-value ref-dep))))
3189         (if (string= id ref)
3190             ;; Yuk!  This is a reference loop.  Make the article be a
3191             ;; root article.
3192             (progn
3193               (mail-header-set-references (car (symbol-value id-dep)) "none")
3194               (setq ref nil))
3195           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3196       (setq ref (gnus-parent-id (mail-header-references header)))
3197       (setq ref-dep (intern (or ref "none") dependencies))
3198       (if (boundp ref-dep)
3199           (setcdr (symbol-value ref-dep)
3200                   (nconc (cdr (symbol-value ref-dep))
3201                          (list (symbol-value id-dep))))
3202         (set ref-dep (list nil (symbol-value id-dep)))))
3203     header))
3204
3205 (defun gnus-build-sparse-threads ()
3206   (let ((headers gnus-newsgroup-headers)
3207         (mail-parse-charset gnus-newsgroup-charset)
3208         (gnus-summary-ignore-duplicates t)
3209         header references generation relations
3210         subject child end new-child date)
3211     ;; First we create an alist of generations/relations, where
3212     ;; generations is how much we trust the relation, and the relation
3213     ;; is parent/child.
3214     (gnus-message 7 "Making sparse threads...")
3215     (save-excursion
3216       (nnheader-set-temp-buffer " *gnus sparse threads*")
3217       (while (setq header (pop headers))
3218         (when (and (setq references (mail-header-references header))
3219                    (not (string= references "")))
3220           (insert references)
3221           (setq child (mail-header-id header)
3222                 subject (mail-header-subject header)
3223                 date (mail-header-date header)
3224                 generation 0)
3225           (while (search-backward ">" nil t)
3226             (setq end (1+ (point)))
3227             (when (search-backward "<" nil t)
3228               (setq new-child (buffer-substring (point) end))
3229               (push (list (incf generation)
3230                           child (setq child new-child)
3231                           subject date)
3232                     relations)))
3233           (when child
3234             (push (list (1+ generation) child nil subject) relations))
3235           (erase-buffer)))
3236       (kill-buffer (current-buffer)))
3237     ;; Sort over trustworthiness.
3238     (mapcar
3239      (lambda (relation)
3240        (when (gnus-dependencies-add-header
3241               (make-full-mail-header
3242                gnus-reffed-article-number
3243                (nth 3 relation) "" (or (nth 4 relation) "")
3244                (nth 1 relation)
3245                (or (nth 2 relation) "") 0 0 "")
3246               gnus-newsgroup-dependencies nil)
3247          (push gnus-reffed-article-number gnus-newsgroup-limit)
3248          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3249          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3250                gnus-newsgroup-reads)
3251          (decf gnus-reffed-article-number)))
3252      (sort relations 'car-less-than-car))
3253     (gnus-message 7 "Making sparse threads...done")))
3254
3255 (defun gnus-build-old-threads ()
3256   ;; Look at all the articles that refer back to old articles, and
3257   ;; fetch the headers for the articles that aren't there.  This will
3258   ;; build complete threads - if the roots haven't been expired by the
3259   ;; server, that is.
3260   (let ((mail-parse-charset gnus-newsgroup-charset)
3261         id heads)
3262     (mapatoms
3263      (lambda (refs)
3264        (when (not (car (symbol-value refs)))
3265          (setq heads (cdr (symbol-value refs)))
3266          (while heads
3267            (if (memq (mail-header-number (caar heads))
3268                      gnus-newsgroup-dormant)
3269                (setq heads (cdr heads))
3270              (setq id (symbol-name refs))
3271              (while (and (setq id (gnus-build-get-header id))
3272                          (not (car (gnus-id-to-thread id)))))
3273              (setq heads nil)))))
3274      gnus-newsgroup-dependencies)))
3275
3276 ;; This function has to be called with point after the article number
3277 ;; on the beginning of the line.
3278 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3279   (let ((eol (gnus-point-at-eol))
3280         (buffer (current-buffer))
3281         header)
3282
3283     ;; overview: [num subject from date id refs chars lines misc]
3284     (unwind-protect
3285         (progn
3286           (narrow-to-region (point) eol)
3287           (unless (eobp)
3288             (forward-char))
3289
3290           (setq header
3291                 (make-full-mail-header
3292                  number                 ; number
3293                  (funcall gnus-decode-encoded-word-function
3294                           (nnheader-nov-field)) ; subject
3295                  (funcall gnus-decode-encoded-word-function
3296                           (nnheader-nov-field)) ; from
3297                  (nnheader-nov-field)   ; date
3298                  (nnheader-nov-read-message-id) ; id
3299                  (nnheader-nov-field)   ; refs
3300                  (nnheader-nov-read-integer) ; chars
3301                  (nnheader-nov-read-integer) ; lines
3302                  (unless (eobp)
3303                    (nnheader-nov-field)) ; misc
3304                  (nnheader-nov-parse-extra)))) ; extra
3305
3306       (widen))
3307
3308     (when gnus-alter-header-function
3309       (funcall gnus-alter-header-function header))
3310     (gnus-dependencies-add-header header dependencies force-new)))
3311
3312 (defun gnus-build-get-header (id)
3313   "Look through the buffer of NOV lines and find the header to ID.
3314 Enter this line into the dependencies hash table, and return
3315 the id of the parent article (if any)."
3316   (let ((deps gnus-newsgroup-dependencies)
3317         found header)
3318     (prog1
3319         (save-excursion
3320           (set-buffer nntp-server-buffer)
3321           (let ((case-fold-search nil))
3322             (goto-char (point-min))
3323             (while (and (not found)
3324                         (search-forward id nil t))
3325               (beginning-of-line)
3326               (setq found (looking-at
3327                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3328                                    (regexp-quote id))))
3329               (or found (beginning-of-line 2)))
3330             (when found
3331               (beginning-of-line)
3332               (and
3333                (setq header (gnus-nov-parse-line
3334                              (read (current-buffer)) deps))
3335                (gnus-parent-id (mail-header-references header))))))
3336       (when header
3337         (let ((number (mail-header-number header)))
3338           (push number gnus-newsgroup-limit)
3339           (push header gnus-newsgroup-headers)
3340           (if (memq number gnus-newsgroup-unselected)
3341               (progn
3342                 (push number gnus-newsgroup-unreads)
3343                 (setq gnus-newsgroup-unselected
3344                       (delq number gnus-newsgroup-unselected)))
3345             (push number gnus-newsgroup-ancient)))))))
3346
3347 (defun gnus-build-all-threads ()
3348   "Read all the headers."
3349   (let ((gnus-summary-ignore-duplicates t)
3350         (mail-parse-charset gnus-newsgroup-charset)
3351         (dependencies gnus-newsgroup-dependencies)
3352         header article)
3353     (save-excursion
3354       (set-buffer nntp-server-buffer)
3355       (let ((case-fold-search nil))
3356         (goto-char (point-min))
3357         (while (not (eobp))
3358           (ignore-errors
3359             (setq article (read (current-buffer))
3360                   header (gnus-nov-parse-line article dependencies)))
3361           (when header
3362             (save-excursion
3363               (set-buffer gnus-summary-buffer)
3364               (push header gnus-newsgroup-headers)
3365               (if (memq (setq article (mail-header-number header))
3366                         gnus-newsgroup-unselected)
3367                   (progn
3368                     (push article gnus-newsgroup-unreads)
3369                     (setq gnus-newsgroup-unselected
3370                           (delq article gnus-newsgroup-unselected)))
3371                 (push article gnus-newsgroup-ancient)))
3372             (forward-line 1)))))))
3373
3374 (defun gnus-summary-update-article-line (article header)
3375   "Update the line for ARTICLE using HEADERS."
3376   (let* ((id (mail-header-id header))
3377          (thread (gnus-id-to-thread id)))
3378     (unless thread
3379       (error "Article in no thread"))
3380     ;; Update the thread.
3381     (setcar thread header)
3382     (gnus-summary-goto-subject article)
3383     (let* ((datal (gnus-data-find-list article))
3384            (data (car datal))
3385            (length (when (cdr datal)
3386                      (- (gnus-data-pos data)
3387                         (gnus-data-pos (cadr datal)))))
3388            (buffer-read-only nil)
3389            (level (gnus-summary-thread-level)))
3390       (gnus-delete-line)
3391       (gnus-summary-insert-line
3392        header level nil (gnus-article-mark article)
3393        (memq article gnus-newsgroup-replied)
3394        (memq article gnus-newsgroup-expirable)
3395        ;; Only insert the Subject string when it's different
3396        ;; from the previous Subject string.
3397        (if (and
3398             gnus-show-threads
3399             (gnus-subject-equal
3400              (condition-case ()
3401                  (mail-header-subject
3402                   (gnus-data-header
3403                    (cadr
3404                     (gnus-data-find-list
3405                      article
3406                      (gnus-data-list t)))))
3407                ;; Error on the side of excessive subjects.
3408                (error ""))
3409              (mail-header-subject header)))
3410            ""
3411          (mail-header-subject header))
3412        nil (cdr (assq article gnus-newsgroup-scored))
3413        (memq article gnus-newsgroup-processable))
3414       (when length
3415         (gnus-data-update-list
3416          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3417
3418 (defun gnus-summary-update-article (article &optional iheader)
3419   "Update ARTICLE in the summary buffer."
3420   (set-buffer gnus-summary-buffer)
3421   (let* ((header (gnus-summary-article-header article))
3422          (id (mail-header-id header))
3423          (data (gnus-data-find article))
3424          (thread (gnus-id-to-thread id))
3425          (references (mail-header-references header))
3426          (parent
3427           (gnus-id-to-thread
3428            (or (gnus-parent-id
3429                 (when (and references
3430                            (not (equal "" references)))
3431                   references))
3432                "none")))
3433          (buffer-read-only nil)
3434          (old (car thread)))
3435     (when thread
3436       (unless iheader
3437         (setcar thread nil)
3438         (when parent
3439           (delq thread parent)))
3440       (if (gnus-summary-insert-subject id header)
3441           ;; Set the (possibly) new article number in the data structure.
3442           (gnus-data-set-number data (gnus-id-to-article id))
3443         (setcar thread old)
3444         nil))))
3445
3446 (defun gnus-rebuild-thread (id &optional line)
3447   "Rebuild the thread containing ID.
3448 If LINE, insert the rebuilt thread starting on line LINE."
3449   (let ((buffer-read-only nil)
3450         old-pos current thread data)
3451     (if (not gnus-show-threads)
3452         (setq thread (list (car (gnus-id-to-thread id))))
3453       ;; Get the thread this article is part of.
3454       (setq thread (gnus-remove-thread id)))
3455     (setq old-pos (gnus-point-at-bol))
3456     (setq current (save-excursion
3457                     (and (zerop (forward-line -1))
3458                          (gnus-summary-article-number))))
3459     ;; If this is a gathered thread, we have to go some re-gathering.
3460     (when (stringp (car thread))
3461       (let ((subject (car thread))
3462             roots thr)
3463         (setq thread (cdr thread))
3464         (while thread
3465           (unless (memq (setq thr (gnus-id-to-thread
3466                                    (gnus-root-id
3467                                     (mail-header-id (caar thread)))))
3468                         roots)
3469             (push thr roots))
3470           (setq thread (cdr thread)))
3471         ;; We now have all (unique) roots.
3472         (if (= (length roots) 1)
3473             ;; All the loose roots are now one solid root.
3474             (setq thread (car roots))
3475           (setq thread (cons subject (gnus-sort-threads roots))))))
3476     (let (threads)
3477       ;; We then insert this thread into the summary buffer.
3478       (when line
3479         (goto-char (point-min))
3480         (forward-line (1- line)))
3481       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3482         (if gnus-show-threads
3483             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3484           (gnus-summary-prepare-unthreaded thread))
3485         (setq data (nreverse gnus-newsgroup-data))
3486         (setq threads gnus-newsgroup-threads))
3487       ;; We splice the new data into the data structure.
3488       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3489       ;;!!! then we want to insert at the beginning of the buffer.
3490       ;;!!! That happens to be true with Gnus now, but that may
3491       ;;!!! change in the future.  Perhaps.
3492       (gnus-data-enter-list
3493        (if line nil current) data (- (point) old-pos))
3494       (setq gnus-newsgroup-threads
3495             (nconc threads gnus-newsgroup-threads))
3496       (gnus-data-compute-positions))))
3497
3498 (defun gnus-number-to-header (number)
3499   "Return the header for article NUMBER."
3500   (let ((headers gnus-newsgroup-headers))
3501     (while (and headers
3502                 (not (= number (mail-header-number (car headers)))))
3503       (pop headers))
3504     (when headers
3505       (car headers))))
3506
3507 (defun gnus-parent-headers (in-headers &optional generation)
3508   "Return the headers of the GENERATIONeth parent of HEADERS."
3509   (unless generation
3510     (setq generation 1))
3511   (let ((parent t)
3512         (headers in-headers)
3513         references)
3514     (while (and parent
3515                 (not (zerop generation))
3516                 (setq references (mail-header-references headers)))
3517       (setq headers (if (and references
3518                              (setq parent (gnus-parent-id references)))
3519                         (car (gnus-id-to-thread parent))
3520                       nil))
3521       (decf generation))
3522     (and (not (eq headers in-headers))
3523          headers)))
3524
3525 (defun gnus-id-to-thread (id)
3526   "Return the (sub-)thread where ID appears."
3527   (gnus-gethash id gnus-newsgroup-dependencies))
3528
3529 (defun gnus-id-to-article (id)
3530   "Return the article number of ID."
3531   (let ((thread (gnus-id-to-thread id)))
3532     (when (and thread
3533                (car thread))
3534       (mail-header-number (car thread)))))
3535
3536 (defun gnus-id-to-header (id)
3537   "Return the article headers of ID."
3538   (car (gnus-id-to-thread id)))
3539
3540 (defun gnus-article-displayed-root-p (article)
3541   "Say whether ARTICLE is a root(ish) article."
3542   (let ((level (gnus-summary-thread-level article))
3543         (refs (mail-header-references  (gnus-summary-article-header article)))
3544         particle)
3545     (cond
3546      ((null level) nil)
3547      ((zerop level) t)
3548      ((null refs) t)
3549      ((null (gnus-parent-id refs)) t)
3550      ((and (= 1 level)
3551            (null (setq particle (gnus-id-to-article
3552                                  (gnus-parent-id refs))))
3553            (null (gnus-summary-thread-level particle)))))))
3554
3555 (defun gnus-root-id (id)
3556   "Return the id of the root of the thread where ID appears."
3557   (let (last-id prev)
3558     (while (and id (setq prev (car (gnus-id-to-thread id))))
3559       (setq last-id id
3560             id (gnus-parent-id (mail-header-references prev))))
3561     last-id))
3562
3563 (defun gnus-articles-in-thread (thread)
3564   "Return the list of articles in THREAD."
3565   (cons (mail-header-number (car thread))
3566         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3567
3568 (defun gnus-remove-thread (id &optional dont-remove)
3569   "Remove the thread that has ID in it."
3570   (let (headers thread last-id)
3571     ;; First go up in this thread until we find the root.
3572     (setq last-id (gnus-root-id id)
3573           headers (message-flatten-list (gnus-id-to-thread last-id)))
3574     ;; We have now found the real root of this thread.  It might have
3575     ;; been gathered into some loose thread, so we have to search
3576     ;; through the threads to find the thread we wanted.
3577     (let ((threads gnus-newsgroup-threads)
3578           sub)
3579       (while threads
3580         (setq sub (car threads))
3581         (if (stringp (car sub))
3582             ;; This is a gathered thread, so we look at the roots
3583             ;; below it to find whether this article is in this
3584             ;; gathered root.
3585             (progn
3586               (setq sub (cdr sub))
3587               (while sub
3588                 (when (member (caar sub) headers)
3589                   (setq thread (car threads)
3590                         threads nil
3591                         sub nil))
3592                 (setq sub (cdr sub))))
3593           ;; It's an ordinary thread, so we check it.
3594           (when (eq (car sub) (car headers))
3595             (setq thread sub
3596                   threads nil)))
3597         (setq threads (cdr threads)))
3598       ;; If this article is in no thread, then it's a root.
3599       (if thread
3600           (unless dont-remove
3601             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3602         (setq thread (gnus-id-to-thread last-id)))
3603       (when thread
3604         (prog1
3605             thread                      ; We return this thread.
3606           (unless dont-remove
3607             (if (stringp (car thread))
3608                 (progn
3609                   ;; If we use dummy roots, then we have to remove the
3610                   ;; dummy root as well.
3611                   (when (eq gnus-summary-make-false-root 'dummy)
3612                     ;; We go to the dummy root by going to
3613                     ;; the first sub-"thread", and then one line up.
3614                     (gnus-summary-goto-article
3615                      (mail-header-number (caadr thread)))
3616                     (forward-line -1)
3617                     (gnus-delete-line)
3618                     (gnus-data-compute-positions))
3619                   (setq thread (cdr thread))
3620                   (while thread
3621                     (gnus-remove-thread-1 (car thread))
3622                     (setq thread (cdr thread))))
3623               (gnus-remove-thread-1 thread))))))))
3624
3625 (defun gnus-remove-thread-1 (thread)
3626   "Remove the thread THREAD recursively."
3627   (let ((number (mail-header-number (pop thread)))
3628         d)
3629     (setq thread (reverse thread))
3630     (while thread
3631       (gnus-remove-thread-1 (pop thread)))
3632     (when (setq d (gnus-data-find number))
3633       (goto-char (gnus-data-pos d))
3634       (gnus-summary-show-thread)
3635       (gnus-data-remove
3636        number
3637        (- (gnus-point-at-bol)
3638           (prog1
3639               (1+ (gnus-point-at-eol))
3640             (gnus-delete-line)))))))
3641
3642 (defun gnus-sort-threads-1 (threads func)
3643   (sort (mapcar (lambda (thread)
3644                   (cons (car thread)
3645                         (and (cdr thread)
3646                              (gnus-sort-threads-1 (cdr thread) func))))
3647                 threads) func))
3648
3649 (defun gnus-sort-threads (threads)
3650   "Sort THREADS."
3651   (if (not gnus-thread-sort-functions)
3652       threads
3653     (gnus-message 8 "Sorting threads...")
3654     (prog1
3655         (gnus-sort-threads-1 
3656          threads 
3657          (gnus-make-sort-function gnus-thread-sort-functions))
3658       (gnus-message 8 "Sorting threads...done"))))
3659
3660 (defun gnus-sort-articles (articles)
3661   "Sort ARTICLES."
3662   (when gnus-article-sort-functions
3663     (gnus-message 7 "Sorting articles...")
3664     (prog1
3665         (setq gnus-newsgroup-headers
3666               (sort articles (gnus-make-sort-function
3667                               gnus-article-sort-functions)))
3668       (gnus-message 7 "Sorting articles...done"))))
3669
3670 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3671 (defmacro gnus-thread-header (thread)
3672   "Return header of first article in THREAD.
3673 Note that THREAD must never, ever be anything else than a variable -
3674 using some other form will lead to serious barfage."
3675   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3676   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3677   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3678         (vector thread) 2))
3679
3680 (defsubst gnus-article-sort-by-number (h1 h2)
3681   "Sort articles by article number."
3682   (< (mail-header-number h1)
3683      (mail-header-number h2)))
3684
3685 (defun gnus-thread-sort-by-number (h1 h2)
3686   "Sort threads by root article number."
3687   (gnus-article-sort-by-number
3688    (gnus-thread-header h1) (gnus-thread-header h2)))
3689
3690 (defsubst gnus-article-sort-by-lines (h1 h2)
3691   "Sort articles by article Lines header."
3692   (< (mail-header-lines h1)
3693      (mail-header-lines h2)))
3694
3695 (defun gnus-thread-sort-by-lines (h1 h2)
3696   "Sort threads by root article Lines header."
3697   (gnus-article-sort-by-lines
3698    (gnus-thread-header h1) (gnus-thread-header h2)))
3699
3700 (defsubst gnus-article-sort-by-chars (h1 h2)
3701   "Sort articles by octet length."
3702   (< (mail-header-chars h1)
3703      (mail-header-chars h2)))
3704
3705 (defun gnus-thread-sort-by-chars (h1 h2)
3706   "Sort threads by root article octet length."
3707   (gnus-article-sort-by-chars
3708    (gnus-thread-header h1) (gnus-thread-header h2)))
3709
3710 (defsubst gnus-article-sort-by-author (h1 h2)
3711   "Sort articles by root author."
3712   (string-lessp
3713    (let ((extract (funcall
3714                    gnus-extract-address-components
3715                    (mail-header-from h1))))
3716      (or (car extract) (cadr extract) ""))
3717    (let ((extract (funcall
3718                    gnus-extract-address-components
3719                    (mail-header-from h2))))
3720      (or (car extract) (cadr extract) ""))))
3721
3722 (defun gnus-thread-sort-by-author (h1 h2)
3723   "Sort threads by root author."
3724   (gnus-article-sort-by-author
3725    (gnus-thread-header h1)  (gnus-thread-header h2)))
3726
3727 (defsubst gnus-article-sort-by-subject (h1 h2)
3728   "Sort articles by root subject."
3729   (string-lessp
3730    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3731    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3732
3733 (defun gnus-thread-sort-by-subject (h1 h2)
3734   "Sort threads by root subject."
3735   (gnus-article-sort-by-subject
3736    (gnus-thread-header h1) (gnus-thread-header h2)))
3737
3738 (defsubst gnus-article-sort-by-date (h1 h2)
3739   "Sort articles by root article date."
3740   (time-less-p
3741    (gnus-date-get-time (mail-header-date h1))
3742    (gnus-date-get-time (mail-header-date h2))))
3743
3744 (defun gnus-thread-sort-by-date (h1 h2)
3745   "Sort threads by root article date."
3746   (gnus-article-sort-by-date
3747    (gnus-thread-header h1) (gnus-thread-header h2)))
3748
3749 (defsubst gnus-article-sort-by-score (h1 h2)
3750   "Sort articles by root article score.
3751 Unscored articles will be counted as having a score of zero."
3752   (> (or (cdr (assq (mail-header-number h1)
3753                     gnus-newsgroup-scored))
3754          gnus-summary-default-score 0)
3755      (or (cdr (assq (mail-header-number h2)
3756                     gnus-newsgroup-scored))
3757          gnus-summary-default-score 0)))
3758
3759 (defun gnus-thread-sort-by-score (h1 h2)
3760   "Sort threads by root article score."
3761   (gnus-article-sort-by-score
3762    (gnus-thread-header h1) (gnus-thread-header h2)))
3763
3764 (defun gnus-thread-sort-by-total-score (h1 h2)
3765   "Sort threads by the sum of all scores in the thread.
3766 Unscored articles will be counted as having a score of zero."
3767   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3768
3769 (defun gnus-thread-total-score (thread)
3770   ;; This function find the total score of THREAD.
3771   (cond ((null thread)
3772          0)
3773         ((consp thread)
3774          (if (stringp (car thread))
3775              (apply gnus-thread-score-function 0
3776                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3777            (gnus-thread-total-score-1 thread)))
3778         (t
3779          (gnus-thread-total-score-1 (list thread)))))
3780
3781 (defun gnus-thread-total-score-1 (root)
3782   ;; This function find the total score of the thread below ROOT.
3783   (setq root (car root))
3784   (apply gnus-thread-score-function
3785          (or (append
3786               (mapcar 'gnus-thread-total-score
3787                       (cdr (gnus-id-to-thread (mail-header-id root))))
3788               (when (> (mail-header-number root) 0)
3789                 (list (or (cdr (assq (mail-header-number root)
3790                                      gnus-newsgroup-scored))
3791                           gnus-summary-default-score 0))))
3792              (list gnus-summary-default-score)
3793              '(0))))
3794
3795 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3796 (defvar gnus-tmp-prev-subject nil)
3797 (defvar gnus-tmp-false-parent nil)
3798 (defvar gnus-tmp-root-expunged nil)
3799 (defvar gnus-tmp-dummy-line nil)
3800
3801 (defvar gnus-tmp-header)
3802 (defun gnus-extra-header (type &optional header)
3803   "Return the extra header of TYPE."
3804   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3805       ""))
3806
3807 (defun gnus-summary-prepare-threads (threads)
3808   "Prepare summary buffer from THREADS and indentation LEVEL.
3809 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3810 or a straight list of headers."
3811   (gnus-message 7 "Generating summary...")
3812
3813   (setq gnus-newsgroup-threads threads)
3814   (beginning-of-line)
3815
3816   (let ((gnus-tmp-level 0)
3817         (default-score (or gnus-summary-default-score 0))
3818         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3819         thread number subject stack state gnus-tmp-gathered beg-match
3820         new-roots gnus-tmp-new-adopts thread-end
3821         gnus-tmp-header gnus-tmp-unread
3822         gnus-tmp-replied gnus-tmp-subject-or-nil
3823         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3824         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3825         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3826
3827     (setq gnus-tmp-prev-subject nil)
3828
3829     (if (vectorp (car threads))
3830         ;; If this is a straight (sic) list of headers, then a
3831         ;; threaded summary display isn't required, so we just create
3832         ;; an unthreaded one.
3833         (gnus-summary-prepare-unthreaded threads)
3834
3835       ;; Do the threaded display.
3836
3837       (while (or threads stack gnus-tmp-new-adopts new-roots)
3838
3839         (if (and (= gnus-tmp-level 0)
3840                  (or (not stack)
3841                      (= (caar stack) 0))
3842                  (not gnus-tmp-false-parent)
3843                  (or gnus-tmp-new-adopts new-roots))
3844             (if gnus-tmp-new-adopts
3845                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3846                       thread (list (car gnus-tmp-new-adopts))
3847                       gnus-tmp-header (caar thread)
3848                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3849               (when new-roots
3850                 (setq thread (list (car new-roots))
3851                       gnus-tmp-header (caar thread)
3852                       new-roots (cdr new-roots))))
3853
3854           (if threads
3855               ;; If there are some threads, we do them before the
3856               ;; threads on the stack.
3857               (setq thread threads
3858                     gnus-tmp-header (caar thread))
3859             ;; There were no current threads, so we pop something off
3860             ;; the stack.
3861             (setq state (car stack)
3862                   gnus-tmp-level (car state)
3863                   thread (cdr state)
3864                   stack (cdr stack)
3865                   gnus-tmp-header (caar thread))))
3866
3867         (setq gnus-tmp-false-parent nil)
3868         (setq gnus-tmp-root-expunged nil)
3869         (setq thread-end nil)
3870
3871         (if (stringp gnus-tmp-header)
3872             ;; The header is a dummy root.
3873             (cond
3874              ((eq gnus-summary-make-false-root 'adopt)
3875               ;; We let the first article adopt the rest.
3876               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3877                                                (cddar thread)))
3878               (setq gnus-tmp-gathered
3879                     (nconc (mapcar
3880                             (lambda (h) (mail-header-number (car h)))
3881                             (cddar thread))
3882                            gnus-tmp-gathered))
3883               (setq thread (cons (list (caar thread)
3884                                        (cadar thread))
3885                                  (cdr thread)))
3886               (setq gnus-tmp-level -1
3887                     gnus-tmp-false-parent t))
3888              ((eq gnus-summary-make-false-root 'empty)
3889               ;; We print adopted articles with empty subject fields.
3890               (setq gnus-tmp-gathered
3891                     (nconc (mapcar
3892                             (lambda (h) (mail-header-number (car h)))
3893                             (cddar thread))
3894                            gnus-tmp-gathered))
3895               (setq gnus-tmp-level -1))
3896              ((eq gnus-summary-make-false-root 'dummy)
3897               ;; We remember that we probably want to output a dummy
3898               ;; root.
3899               (setq gnus-tmp-dummy-line gnus-tmp-header)
3900               (setq gnus-tmp-prev-subject gnus-tmp-header))
3901              (t
3902               ;; We do not make a root for the gathered
3903               ;; sub-threads at all.
3904               (setq gnus-tmp-level -1)))
3905
3906           (setq number (mail-header-number gnus-tmp-header)
3907                 subject (mail-header-subject gnus-tmp-header))
3908
3909           (cond
3910            ;; If the thread has changed subject, we might want to make
3911            ;; this subthread into a root.
3912            ((and (null gnus-thread-ignore-subject)
3913                  (not (zerop gnus-tmp-level))
3914                  gnus-tmp-prev-subject
3915                  (not (inline
3916                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3917             (setq new-roots (nconc new-roots (list (car thread)))
3918                   thread-end t
3919                   gnus-tmp-header nil))
3920            ;; If the article lies outside the current limit,
3921            ;; then we do not display it.
3922            ((not (memq number gnus-newsgroup-limit))
3923             (setq gnus-tmp-gathered
3924                   (nconc (mapcar
3925                           (lambda (h) (mail-header-number (car h)))
3926                           (cdar thread))
3927                          gnus-tmp-gathered))
3928             (setq gnus-tmp-new-adopts (if (cdar thread)
3929                                           (append gnus-tmp-new-adopts
3930                                                   (cdar thread))
3931                                         gnus-tmp-new-adopts)
3932                   thread-end t
3933                   gnus-tmp-header nil)
3934             (when (zerop gnus-tmp-level)
3935               (setq gnus-tmp-root-expunged t)))
3936            ;; Perhaps this article is to be marked as read?
3937            ((and gnus-summary-mark-below
3938                  (< (or (cdr (assq number gnus-newsgroup-scored))
3939                         default-score)
3940                     gnus-summary-mark-below)
3941                  ;; Don't touch sparse articles.
3942                  (not (gnus-summary-article-sparse-p number))
3943                  (not (gnus-summary-article-ancient-p number)))
3944             (setq gnus-newsgroup-unreads
3945                   (delq number gnus-newsgroup-unreads))
3946             (if gnus-newsgroup-auto-expire
3947                 (push number gnus-newsgroup-expirable)
3948               (push (cons number gnus-low-score-mark)
3949                     gnus-newsgroup-reads))))
3950
3951           (when gnus-tmp-header
3952             ;; We may have an old dummy line to output before this
3953             ;; article.
3954             (when (and gnus-tmp-dummy-line
3955                        (gnus-subject-equal
3956                         gnus-tmp-dummy-line
3957                         (mail-header-subject gnus-tmp-header)))
3958               (gnus-summary-insert-dummy-line
3959                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3960               (setq gnus-tmp-dummy-line nil))
3961
3962             ;; Compute the mark.
3963             (setq gnus-tmp-unread (gnus-article-mark number))
3964
3965             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3966                                   gnus-tmp-header gnus-tmp-level)
3967                   gnus-newsgroup-data)
3968
3969             ;; Actually insert the line.
3970             (setq
3971              gnus-tmp-subject-or-nil
3972              (cond
3973               ((and gnus-thread-ignore-subject
3974                     gnus-tmp-prev-subject
3975                     (not (inline (gnus-subject-equal
3976                                   gnus-tmp-prev-subject subject))))
3977                subject)
3978               ((zerop gnus-tmp-level)
3979                (if (and (eq gnus-summary-make-false-root 'empty)
3980                         (memq number gnus-tmp-gathered)
3981                         gnus-tmp-prev-subject
3982                         (inline (gnus-subject-equal
3983                                  gnus-tmp-prev-subject subject)))
3984                    gnus-summary-same-subject
3985                  subject))
3986               (t gnus-summary-same-subject)))
3987             (if (and (eq gnus-summary-make-false-root 'adopt)
3988                      (= gnus-tmp-level 1)
3989                      (memq number gnus-tmp-gathered))
3990                 (setq gnus-tmp-opening-bracket ?\<
3991                       gnus-tmp-closing-bracket ?\>)
3992               (setq gnus-tmp-opening-bracket ?\[
3993                     gnus-tmp-closing-bracket ?\]))
3994             (setq
3995              gnus-tmp-indentation
3996              (aref gnus-thread-indent-array gnus-tmp-level)
3997              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3998              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3999                                 gnus-summary-default-score 0)
4000              gnus-tmp-score-char
4001              (if (or (null gnus-summary-default-score)
4002                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4003                          gnus-summary-zcore-fuzz))
4004                  ?  ;Whitespace
4005                (if (< gnus-tmp-score gnus-summary-default-score)
4006                    gnus-score-below-mark gnus-score-over-mark))
4007              gnus-tmp-replied
4008              (cond ((memq number gnus-newsgroup-processable)
4009                     gnus-process-mark)
4010                    ((memq number gnus-newsgroup-cached)
4011                     gnus-cached-mark)
4012                    ((memq number gnus-newsgroup-replied)
4013                     gnus-replied-mark)
4014                    ((memq number gnus-newsgroup-saved)
4015                     gnus-saved-mark)
4016                    (t gnus-unread-mark))
4017              gnus-tmp-from (mail-header-from gnus-tmp-header)
4018              gnus-tmp-name
4019              (cond
4020               ((string-match "<[^>]+> *$" gnus-tmp-from)
4021                (setq beg-match (match-beginning 0))
4022                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
4023                         (substring gnus-tmp-from (1+ (match-beginning 0))
4024                                    (1- (match-end 0))))
4025                    (substring gnus-tmp-from 0 beg-match)))
4026               ((string-match "(.+)" gnus-tmp-from)
4027                (substring gnus-tmp-from
4028                           (1+ (match-beginning 0)) (1- (match-end 0))))
4029               (t gnus-tmp-from)))
4030             (when (string= gnus-tmp-name "")
4031               (setq gnus-tmp-name gnus-tmp-from))
4032             (unless (numberp gnus-tmp-lines)
4033               (setq gnus-tmp-lines 0))
4034             (gnus-put-text-property
4035              (point)
4036              (progn (eval gnus-summary-line-format-spec) (point))
4037              'gnus-number number)
4038             (when gnus-visual-p
4039               (forward-line -1)
4040               (gnus-run-hooks 'gnus-summary-update-hook)
4041               (forward-line 1))
4042
4043             (setq gnus-tmp-prev-subject subject)))
4044
4045         (when (nth 1 thread)
4046           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4047         (incf gnus-tmp-level)
4048         (setq threads (if thread-end nil (cdar thread)))
4049         (unless threads
4050           (setq gnus-tmp-level 0)))))
4051   (gnus-message 7 "Generating summary...done"))
4052
4053 (defun gnus-summary-prepare-unthreaded (headers)
4054   "Generate an unthreaded summary buffer based on HEADERS."
4055   (let (header number mark)
4056
4057     (beginning-of-line)
4058
4059     (while headers
4060       ;; We may have to root out some bad articles...
4061       (when (memq (setq number (mail-header-number
4062                                 (setq header (pop headers))))
4063                   gnus-newsgroup-limit)
4064         ;; Mark article as read when it has a low score.
4065         (when (and gnus-summary-mark-below
4066                    (< (or (cdr (assq number gnus-newsgroup-scored))
4067                           gnus-summary-default-score 0)
4068                       gnus-summary-mark-below)
4069                    (not (gnus-summary-article-ancient-p number)))
4070           (setq gnus-newsgroup-unreads
4071                 (delq number gnus-newsgroup-unreads))
4072           (if gnus-newsgroup-auto-expire
4073               (push number gnus-newsgroup-expirable)
4074             (push (cons number gnus-low-score-mark)
4075                   gnus-newsgroup-reads)))
4076
4077         (setq mark (gnus-article-mark number))
4078         (push (gnus-data-make number mark (1+ (point)) header 0)
4079               gnus-newsgroup-data)
4080         (gnus-summary-insert-line
4081          header 0 number
4082          mark (memq number gnus-newsgroup-replied)
4083          (memq number gnus-newsgroup-expirable)
4084          (mail-header-subject header) nil
4085          (cdr (assq number gnus-newsgroup-scored))
4086          (memq number gnus-newsgroup-processable))))))
4087
4088 (defun gnus-summary-remove-list-identifiers ()
4089   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4090   (let ((regexp (if (stringp gnus-list-identifiers)
4091                     gnus-list-identifiers
4092                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4093     (dolist (header gnus-newsgroup-headers)
4094       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp 
4095                                   " *\\)\\)+\\(Re: +\\)?\\)")
4096                           (mail-header-subject header))
4097         (mail-header-set-subject
4098          header (concat (substring (mail-header-subject header)
4099                                    0 (match-beginning 1))
4100                         (or
4101                          (match-string 3 (mail-header-subject header))
4102                          (match-string 5 (mail-header-subject header)))
4103                         (substring (mail-header-subject header)
4104                                    (match-end 1))))))))
4105
4106 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4107   "Select newsgroup GROUP.
4108 If READ-ALL is non-nil, all articles in the group are selected.
4109 If SELECT-ARTICLES, only select those articles from GROUP."
4110   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4111          ;;!!! Dirty hack; should be removed.
4112          (gnus-summary-ignore-duplicates
4113           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4114               t
4115             gnus-summary-ignore-duplicates))
4116          (info (nth 2 entry))
4117          articles fetched-articles cached)
4118
4119     (unless (gnus-check-server
4120              (setq gnus-current-select-method
4121                    (gnus-find-method-for-group group)))
4122       (error "Couldn't open server"))
4123
4124     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4125         (gnus-activate-group group)     ; Or we can activate it...
4126         (progn                          ; Or we bug out.
4127           (when (equal major-mode 'gnus-summary-mode)
4128             (kill-buffer (current-buffer)))
4129           (error "Couldn't request group %s: %s"
4130                  group (gnus-status-message group))))
4131
4132     (unless (gnus-request-group group t)
4133       (when (equal major-mode 'gnus-summary-mode)
4134         (kill-buffer (current-buffer)))
4135       (error "Couldn't request group %s: %s"
4136              group (gnus-status-message group)))
4137
4138     (setq gnus-newsgroup-name group)
4139     (setq gnus-newsgroup-unselected nil)
4140     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4141     (gnus-summary-setup-default-charset)
4142
4143     ;; Adjust and set lists of article marks.
4144     (when info
4145       (gnus-adjust-marked-articles info))
4146
4147     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4148     (when (gnus-virtual-group-p group)
4149       (setq cached gnus-newsgroup-cached))
4150
4151     (setq gnus-newsgroup-unreads
4152           (gnus-set-difference
4153            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4154            gnus-newsgroup-dormant))
4155
4156     (setq gnus-newsgroup-processable nil)
4157
4158     (gnus-update-read-articles group gnus-newsgroup-unreads)
4159
4160     (if (setq articles select-articles)
4161         (setq gnus-newsgroup-unselected
4162               (gnus-sorted-intersection
4163                gnus-newsgroup-unreads
4164                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4165       (setq articles (gnus-articles-to-read group read-all)))
4166
4167     (cond
4168      ((null articles)
4169       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4170       'quit)
4171      ((eq articles 0) nil)
4172      (t
4173       ;; Init the dependencies hash table.
4174       (setq gnus-newsgroup-dependencies
4175             (gnus-make-hashtable (length articles)))
4176       (gnus-set-global-variables)
4177       ;; Retrieve the headers and read them in.
4178       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4179       (setq gnus-newsgroup-headers
4180             (if (eq 'nov
4181                     (setq gnus-headers-retrieved-by
4182                           (gnus-retrieve-headers
4183                            articles gnus-newsgroup-name
4184                            ;; We might want to fetch old headers, but
4185                            ;; not if there is only 1 article.
4186                            (and (or (and
4187                                      (not (eq gnus-fetch-old-headers 'some))
4188                                      (not (numberp gnus-fetch-old-headers)))
4189                                     (> (length articles) 1))
4190                                 gnus-fetch-old-headers))))
4191                 (gnus-get-newsgroup-headers-xover
4192                  articles nil nil gnus-newsgroup-name t)
4193               (gnus-get-newsgroup-headers)))
4194       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4195
4196       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4197       (when cached
4198         (setq gnus-newsgroup-cached cached))
4199
4200       ;; Suppress duplicates?
4201       (when gnus-suppress-duplicates
4202         (gnus-dup-suppress-articles))
4203
4204       ;; Set the initial limit.
4205       (setq gnus-newsgroup-limit (copy-sequence articles))
4206       ;; Remove canceled articles from the list of unread articles.
4207       (setq gnus-newsgroup-unreads
4208             (gnus-set-sorted-intersection
4209              gnus-newsgroup-unreads
4210              (setq fetched-articles
4211                    (mapcar (lambda (headers) (mail-header-number headers))
4212                            gnus-newsgroup-headers))))
4213       ;; Removed marked articles that do not exist.
4214       (gnus-update-missing-marks
4215        (gnus-sorted-complement fetched-articles articles))
4216       ;; We might want to build some more threads first.
4217       (when (and gnus-fetch-old-headers
4218                  (eq gnus-headers-retrieved-by 'nov))
4219         (if (eq gnus-fetch-old-headers 'invisible)
4220             (gnus-build-all-threads)
4221           (gnus-build-old-threads)))
4222       ;; Let the Gnus agent mark articles as read.
4223       (when gnus-agent
4224         (gnus-agent-get-undownloaded-list))
4225       ;; Remove list identifiers from subject
4226       (when gnus-list-identifiers
4227         (gnus-summary-remove-list-identifiers))
4228       ;; Check whether auto-expire is to be done in this group.
4229       (setq gnus-newsgroup-auto-expire
4230             (gnus-group-auto-expirable-p group))
4231       ;; Set up the article buffer now, if necessary.
4232       (unless gnus-single-article-buffer
4233         (gnus-article-setup-buffer))
4234       ;; First and last article in this newsgroup.
4235       (when gnus-newsgroup-headers
4236         (setq gnus-newsgroup-begin
4237               (mail-header-number (car gnus-newsgroup-headers))
4238               gnus-newsgroup-end
4239               (mail-header-number
4240                (gnus-last-element gnus-newsgroup-headers))))
4241       ;; GROUP is successfully selected.
4242       (or gnus-newsgroup-headers t)))))
4243
4244 (defun gnus-articles-to-read (group &optional read-all)
4245   "Find out what articles the user wants to read."
4246   (let* ((articles
4247           ;; Select all articles if `read-all' is non-nil, or if there
4248           ;; are no unread articles.
4249           (if (or read-all
4250                   (and (zerop (length gnus-newsgroup-marked))
4251                        (zerop (length gnus-newsgroup-unreads)))
4252                   (eq (gnus-group-find-parameter group 'display)
4253                       'all))
4254               (or
4255                (gnus-uncompress-range (gnus-active group))
4256                (gnus-cache-articles-in-group group))
4257             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4258                           (copy-sequence gnus-newsgroup-unreads))
4259                   '<)))
4260          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4261          (scored (length scored-list))
4262          (number (length articles))
4263          (marked (+ (length gnus-newsgroup-marked)
4264                     (length gnus-newsgroup-dormant)))
4265          (select
4266           (cond
4267            ((numberp read-all)
4268             read-all)
4269            (t
4270             (condition-case ()
4271                 (cond
4272                  ((and (or (<= scored marked) (= scored number))
4273                        (numberp gnus-large-newsgroup)
4274                        (> number gnus-large-newsgroup))
4275                   (let ((input
4276                          (read-string
4277                           (format
4278                            "How many articles from %s (default %d): "
4279                            (gnus-limit-string gnus-newsgroup-name 35)
4280                            number))))
4281                     (if (string-match "^[ \t]*$" input) number input)))
4282                  ((and (> scored marked) (< scored number)
4283                        (> (- scored number) 20))
4284                   (let ((input
4285                          (read-string
4286                           (format "%s %s (%d scored, %d total): "
4287                                   "How many articles from"
4288                                   group scored number))))
4289                     (if (string-match "^[ \t]*$" input)
4290                         number input)))
4291                  (t number))
4292               (quit nil))))))
4293     (setq select (if (stringp select) (string-to-number select) select))
4294     (if (or (null select) (zerop select))
4295         select
4296       (if (and (not (zerop scored)) (<= (abs select) scored))
4297           (progn
4298             (setq articles (sort scored-list '<))
4299             (setq number (length articles)))
4300         (setq articles (copy-sequence articles)))
4301
4302       (when (< (abs select) number)
4303         (if (< select 0)
4304             ;; Select the N oldest articles.
4305             (setcdr (nthcdr (1- (abs select)) articles) nil)
4306           ;; Select the N most recent articles.
4307           (setq articles (nthcdr (- number select) articles))))
4308       (setq gnus-newsgroup-unselected
4309             (gnus-sorted-intersection
4310              gnus-newsgroup-unreads
4311              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4312       (when gnus-alter-articles-to-read-function
4313         (setq gnus-newsgroup-unreads
4314               (sort 
4315                (funcall gnus-alter-articles-to-read-function
4316                         gnus-newsgroup-name gnus-newsgroup-unreads)
4317                '<)))
4318       articles)))
4319
4320 (defun gnus-killed-articles (killed articles)
4321   (let (out)
4322     (while articles
4323       (when (inline (gnus-member-of-range (car articles) killed))
4324         (push (car articles) out))
4325       (setq articles (cdr articles)))
4326     out))
4327
4328 (defun gnus-uncompress-marks (marks)
4329   "Uncompress the mark ranges in MARKS."
4330   (let ((uncompressed '(score bookmark))
4331         out)
4332     (while marks
4333       (if (memq (caar marks) uncompressed)
4334           (push (car marks) out)
4335         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4336       (setq marks (cdr marks)))
4337     out))
4338
4339 (defun gnus-adjust-marked-articles (info)
4340   "Set all article lists and remove all marks that are no longer valid."
4341   (let* ((marked-lists (gnus-info-marks info))
4342          (active (gnus-active (gnus-info-group info)))
4343          (min (car active))
4344          (max (cdr active))
4345          (types gnus-article-mark-lists)
4346          (uncompressed '(score bookmark killed))
4347          marks var articles article mark)
4348
4349     (while marked-lists
4350       (setq marks (pop marked-lists))
4351       (set (setq var (intern (format "gnus-newsgroup-%s"
4352                                      (car (rassq (setq mark (car marks))
4353                                                  types)))))
4354            (if (memq (car marks) uncompressed) (cdr marks)
4355              (gnus-uncompress-range (cdr marks))))
4356
4357       (setq articles (symbol-value var))
4358
4359       ;; All articles have to be subsets of the active articles.
4360       (cond
4361        ;; Adjust "simple" lists.
4362        ((memq mark '(tick dormant expire reply save))
4363         (while articles
4364           (when (or (< (setq article (pop articles)) min) (> article max))
4365             (set var (delq article (symbol-value var))))))
4366        ;; Adjust assocs.
4367        ((memq mark uncompressed)
4368         (when (not (listp (cdr (symbol-value var))))
4369           (set var (list (symbol-value var))))
4370         (when (not (listp (cdr articles)))
4371           (setq articles (list articles)))
4372         (while articles
4373           (when (or (not (consp (setq article (pop articles))))
4374                     (< (car article) min)
4375                     (> (car article) max))
4376             (set var (delq article (symbol-value var))))))))))
4377
4378 (defun gnus-update-missing-marks (missing)
4379   "Go through the list of MISSING articles and remove them from the mark lists."
4380   (when missing
4381     (let ((types gnus-article-mark-lists)
4382           var m)
4383       ;; Go through all types.
4384       (while types
4385         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4386         (when (symbol-value var)
4387           ;; This list has articles.  So we delete all missing articles
4388           ;; from it.
4389           (setq m missing)
4390           (while m
4391             (set var (delq (pop m) (symbol-value var)))))))))
4392
4393 (defun gnus-update-marks ()
4394   "Enter the various lists of marked articles into the newsgroup info list."
4395   (let ((types gnus-article-mark-lists)
4396         (info (gnus-get-info gnus-newsgroup-name))
4397         (uncompressed '(score bookmark killed))
4398         type list newmarked symbol delta-marks)
4399     (when info
4400       ;; Add all marks lists to the list of marks lists.
4401       (while (setq type (pop types))
4402         (setq list (symbol-value
4403                     (setq symbol
4404                           (intern (format "gnus-newsgroup-%s"
4405                                           (car type))))))
4406
4407         (when list
4408           ;; Get rid of the entries of the articles that have the
4409           ;; default score.
4410           (when (and (eq (cdr type) 'score)
4411                      gnus-save-score
4412                      list)
4413             (let* ((arts list)
4414                    (prev (cons nil list))
4415                    (all prev))
4416               (while arts
4417                 (if (or (not (consp (car arts)))
4418                         (= (cdar arts) gnus-summary-default-score))
4419                     (setcdr prev (cdr arts))
4420                   (setq prev arts))
4421                 (setq arts (cdr arts)))
4422               (setq list (cdr all)))))
4423
4424         (unless (memq (cdr type) uncompressed)
4425           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4426        
4427         (when (gnus-check-backend-function
4428                'request-set-mark gnus-newsgroup-name)
4429           ;; propagate flags to server, with the following exceptions:
4430           ;; uncompressed:s are not proper flags (they are cons cells)
4431           ;; cache is a internal gnus flag
4432           ;; download are local to one gnus installation (well)
4433           ;; unsend are for nndraft groups only
4434           ;; xxx: generality of this?  this suits nnimap anyway
4435           (unless (memq (cdr type) (append '(cache download unsend)
4436                                            uncompressed))
4437             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4438                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4439                    (add (gnus-remove-from-range
4440                          (gnus-copy-sequence list) old)))
4441               (when add
4442                 (push (list add 'add (list (cdr type))) delta-marks))
4443               (when del
4444                 (push (list del 'del (list (cdr type))) delta-marks)))))
4445           
4446         (when list
4447           (push (cons (cdr type) list) newmarked)))
4448
4449       (when delta-marks
4450         (unless (gnus-check-group gnus-newsgroup-name)
4451           (error "Can't open server for %s" gnus-newsgroup-name))
4452         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4453           
4454       ;; Enter these new marks into the info of the group.
4455       (if (nthcdr 3 info)
4456           (setcar (nthcdr 3 info) newmarked)
4457         ;; Add the marks lists to the end of the info.
4458         (when newmarked
4459           (setcdr (nthcdr 2 info) (list newmarked))))
4460
4461       ;; Cut off the end of the info if there's nothing else there.
4462       (let ((i 5))
4463         (while (and (> i 2)
4464                     (not (nth i info)))
4465           (when (nthcdr (decf i) info)
4466             (setcdr (nthcdr i info) nil)))))))
4467
4468 (defun gnus-set-mode-line (where)
4469   "Set the mode line of the article or summary buffers.
4470 If WHERE is `summary', the summary mode line format will be used."
4471   ;; Is this mode line one we keep updated?
4472   (when (and (memq where gnus-updated-mode-lines)
4473              (symbol-value
4474               (intern (format "gnus-%s-mode-line-format-spec" where))))
4475     (let (mode-string)
4476       (save-excursion
4477         ;; We evaluate this in the summary buffer since these
4478         ;; variables are buffer-local to that buffer.
4479         (set-buffer gnus-summary-buffer)
4480         ;; We bind all these variables that are used in the `eval' form
4481         ;; below.
4482         (let* ((mformat (symbol-value
4483                          (intern
4484                           (format "gnus-%s-mode-line-format-spec" where))))
4485                (gnus-tmp-group-name (gnus-group-name-decode 
4486                                      gnus-newsgroup-name
4487                                      (gnus-group-name-charset 
4488                                       nil
4489                                       gnus-newsgroup-name)))
4490                (gnus-tmp-article-number (or gnus-current-article 0))
4491                (gnus-tmp-unread gnus-newsgroup-unreads)
4492                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4493                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4494                (gnus-tmp-unread-and-unselected
4495                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4496                             (zerop gnus-tmp-unselected))
4497                        "")
4498                       ((zerop gnus-tmp-unselected)
4499                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4500                       (t (format "{%d(+%d) more}"
4501                                  gnus-tmp-unread-and-unticked
4502                                  gnus-tmp-unselected))))
4503                (gnus-tmp-subject
4504                 (if (and gnus-current-headers
4505                          (vectorp gnus-current-headers))
4506                     (gnus-mode-string-quote
4507                      (mail-header-subject gnus-current-headers))
4508                   ""))
4509                bufname-length max-len
4510                gnus-tmp-header);; passed as argument to any user-format-funcs
4511           (setq mode-string (eval mformat))
4512           (setq bufname-length (if (string-match "%b" mode-string)
4513                                    (- (length
4514                                        (buffer-name
4515                                         (if (eq where 'summary)
4516                                             nil
4517                                           (get-buffer gnus-article-buffer))))
4518                                       2)
4519                                  0))
4520           (setq max-len (max 4 (if gnus-mode-non-string-length
4521                                    (- (window-width)
4522                                       gnus-mode-non-string-length
4523                                       bufname-length)
4524                                  (length mode-string))))
4525           ;; We might have to chop a bit of the string off...
4526           (when (> (length mode-string) max-len)
4527             (setq mode-string
4528                   (concat (truncate-string-to-width mode-string (- max-len 3))
4529                           "...")))
4530           ;; Pad the mode string a bit.
4531           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4532       ;; Update the mode line.
4533       (setq mode-line-buffer-identification
4534             (gnus-mode-line-buffer-identification (list mode-string)))
4535       (set-buffer-modified-p t))))
4536
4537 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4538   "Go through the HEADERS list and add all Xrefs to a hash table.
4539 The resulting hash table is returned, or nil if no Xrefs were found."
4540   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4541          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4542          (xref-hashtb (gnus-make-hashtable))
4543          start group entry number xrefs header)
4544     (while headers
4545       (setq header (pop headers))
4546       (when (and (setq xrefs (mail-header-xref header))
4547                  (not (memq (setq number (mail-header-number header))
4548                             unreads)))
4549         (setq start 0)
4550         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4551           (setq start (match-end 0))
4552           (setq group (if prefix
4553                           (concat prefix (substring xrefs (match-beginning 1)
4554                                                     (match-end 1)))
4555                         (substring xrefs (match-beginning 1) (match-end 1))))
4556           (setq number
4557                 (string-to-int (substring xrefs (match-beginning 2)
4558                                           (match-end 2))))
4559           (if (setq entry (gnus-gethash group xref-hashtb))
4560               (setcdr entry (cons number (cdr entry)))
4561             (gnus-sethash group (cons number nil) xref-hashtb)))))
4562     (and start xref-hashtb)))
4563
4564 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4565   "Look through all the headers and mark the Xrefs as read."
4566   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4567         name entry info xref-hashtb idlist method nth4)
4568     (save-excursion
4569       (set-buffer gnus-group-buffer)
4570       (when (setq xref-hashtb
4571                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4572         (mapatoms
4573          (lambda (group)
4574            (unless (string= from-newsgroup (setq name (symbol-name group)))
4575              (setq idlist (symbol-value group))
4576              ;; Dead groups are not updated.
4577              (and (prog1
4578                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4579                             info (nth 2 entry))
4580                     (when (stringp (setq nth4 (gnus-info-method info)))
4581                       (setq nth4 (gnus-server-to-method nth4))))
4582                   ;; Only do the xrefs if the group has the same
4583                   ;; select method as the group we have just read.
4584                   (or (gnus-methods-equal-p
4585                        nth4 (gnus-find-method-for-group from-newsgroup))
4586                       virtual
4587                       (equal nth4 (setq method (gnus-find-method-for-group
4588                                                 from-newsgroup)))
4589                       (and (equal (car nth4) (car method))
4590                            (equal (nth 1 nth4) (nth 1 method))))
4591                   gnus-use-cross-reference
4592                   (or (not (eq gnus-use-cross-reference t))
4593                       virtual
4594                       ;; Only do cross-references on subscribed
4595                       ;; groups, if that is what is wanted.
4596                       (<= (gnus-info-level info) gnus-level-subscribed))
4597                   (gnus-group-make-articles-read name idlist))))
4598          xref-hashtb)))))
4599
4600 (defun gnus-compute-read-articles (group articles)
4601   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4602          (info (nth 2 entry))
4603          (active (gnus-active group))
4604          ninfo)
4605     (when entry
4606       ;; First peel off all invalid article numbers.
4607       (when active
4608         (let ((ids articles)
4609               id first)
4610           (while (setq id (pop ids))
4611             (when (and first (> id (cdr active)))
4612               ;; We'll end up in this situation in one particular
4613               ;; obscure situation.  If you re-scan a group and get
4614               ;; a new article that is cross-posted to a different
4615               ;; group that has not been re-scanned, you might get
4616               ;; crossposted article that has a higher number than
4617               ;; Gnus believes possible.  So we re-activate this
4618               ;; group as well.  This might mean doing the
4619               ;; crossposting thingy will *increase* the number
4620               ;; of articles in some groups.  Tsk, tsk.
4621               (setq active (or (gnus-activate-group group) active)))
4622             (when (or (> id (cdr active))
4623                       (< id (car active)))
4624               (setq articles (delq id articles))))))
4625       ;; If the read list is nil, we init it.
4626       (if (and active
4627                (null (gnus-info-read info))
4628                (> (car active) 1))
4629           (setq ninfo (cons 1 (1- (car active))))
4630         (setq ninfo (gnus-info-read info)))
4631       ;; Then we add the read articles to the range.
4632       (gnus-add-to-range
4633        ninfo (setq articles (sort articles '<))))))
4634
4635 (defun gnus-group-make-articles-read (group articles)
4636   "Update the info of GROUP to say that ARTICLES are read."
4637   (let* ((num 0)
4638          (entry (gnus-gethash group gnus-newsrc-hashtb))
4639          (info (nth 2 entry))
4640          (active (gnus-active group))
4641          range)
4642     (when entry
4643       (setq range (gnus-compute-read-articles group articles))
4644       (save-excursion
4645         (set-buffer gnus-group-buffer)
4646         (gnus-undo-register
4647           `(progn
4648              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4649              (gnus-info-set-read ',info ',(gnus-info-read info))
4650              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4651              (gnus-group-update-group ,group t))))
4652       ;; Add the read articles to the range.
4653       (gnus-info-set-read info range)
4654       ;; Then we have to re-compute how many unread
4655       ;; articles there are in this group.
4656       (when active
4657         (cond
4658          ((not range)
4659           (setq num (- (1+ (cdr active)) (car active))))
4660          ((not (listp (cdr range)))
4661           (setq num (- (cdr active) (- (1+ (cdr range))
4662                                        (car range)))))
4663          (t
4664           (while range
4665             (if (numberp (car range))
4666                 (setq num (1+ num))
4667               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4668             (setq range (cdr range)))
4669           (setq num (- (cdr active) num))))
4670         ;; Update the number of unread articles.
4671         (setcar entry num)
4672         ;; Update the group buffer.
4673         (gnus-group-update-group group t)))))
4674
4675 (defvar gnus-newsgroup-none-id 0)
4676
4677 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4678   (let ((cur nntp-server-buffer)
4679         (dependencies
4680          (or dependencies
4681              (save-excursion (set-buffer gnus-summary-buffer)
4682                              gnus-newsgroup-dependencies)))
4683         headers id end ref
4684         (mail-parse-charset gnus-newsgroup-charset)
4685         (mail-parse-ignored-charsets
4686          (save-excursion (condition-case nil
4687                              (set-buffer gnus-summary-buffer)
4688                            (error))
4689                          gnus-newsgroup-ignored-charsets)))
4690     (save-excursion
4691       (set-buffer nntp-server-buffer)
4692       ;; Translate all TAB characters into SPACE characters.
4693       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4694       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4695       (gnus-run-hooks 'gnus-parse-headers-hook)
4696       (let ((case-fold-search t)
4697             in-reply-to header p lines chars)
4698         (goto-char (point-min))
4699         ;; Search to the beginning of the next header.  Error messages
4700         ;; do not begin with 2 or 3.
4701         (while (re-search-forward "^[23][0-9]+ " nil t)
4702           (setq id nil
4703                 ref nil)
4704           ;; This implementation of this function, with nine
4705           ;; search-forwards instead of the one re-search-forward and
4706           ;; a case (which basically was the old function) is actually
4707           ;; about twice as fast, even though it looks messier.  You
4708           ;; can't have everything, I guess.  Speed and elegance
4709           ;; doesn't always go hand in hand.
4710           (setq
4711            header
4712            (vector
4713             ;; Number.
4714             (prog1
4715                 (read cur)
4716               (end-of-line)
4717               (setq p (point))
4718               (narrow-to-region (point)
4719                                 (or (and (search-forward "\n.\n" nil t)
4720                                          (- (point) 2))
4721                                     (point))))
4722             ;; Subject.
4723             (progn
4724               (goto-char p)
4725               (if (search-forward "\nsubject: " nil t)
4726                   (funcall gnus-decode-encoded-word-function
4727                            (nnheader-header-value))
4728                 "(none)"))
4729             ;; From.
4730             (progn
4731               (goto-char p)
4732               (if (search-forward "\nfrom: " nil t)
4733                   (funcall gnus-decode-encoded-word-function
4734                            (nnheader-header-value))
4735                 "(nobody)"))
4736             ;; Date.
4737             (progn
4738               (goto-char p)
4739               (if (search-forward "\ndate: " nil t)
4740                   (nnheader-header-value) ""))
4741             ;; Message-ID.
4742             (progn
4743               (goto-char p)
4744               (setq id (if (re-search-forward
4745                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4746                            ;; We do it this way to make sure the Message-ID
4747                            ;; is (somewhat) syntactically valid.
4748                            (buffer-substring (match-beginning 1)
4749                                              (match-end 1))
4750                          ;; If there was no message-id, we just fake one
4751                          ;; to make subsequent routines simpler.
4752                          (nnheader-generate-fake-message-id))))
4753             ;; References.
4754             (progn
4755               (goto-char p)
4756               (if (search-forward "\nreferences: " nil t)
4757                   (progn
4758                     (setq end (point))
4759                     (prog1
4760                         (nnheader-header-value)
4761                       (setq ref
4762                             (buffer-substring
4763                              (progn
4764                                (end-of-line)
4765                                (search-backward ">" end t)
4766                                (1+ (point)))
4767                              (progn
4768                                (search-backward "<" end t)
4769                                (point))))))
4770                 ;; Get the references from the in-reply-to header if there
4771                 ;; were no references and the in-reply-to header looks
4772                 ;; promising.
4773                 (if (and (search-forward "\nin-reply-to: " nil t)
4774                          (setq in-reply-to (nnheader-header-value))
4775                          (string-match "<[^>]+>" in-reply-to))
4776                     (let (ref2)
4777                       (setq ref (substring in-reply-to (match-beginning 0)
4778                                            (match-end 0)))
4779                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4780                         (setq ref2 (substring in-reply-to (match-beginning 0)
4781                                               (match-end 0)))
4782                         (when (> (length ref2) (length ref))
4783                           (setq ref ref2)))
4784                       ref)
4785                   (setq ref nil))))
4786             ;; Chars.
4787             (progn
4788               (goto-char p)
4789               (if (search-forward "\nchars: " nil t)
4790                   (if (numberp (setq chars (ignore-errors (read cur))))
4791                       chars 0)
4792                 0))
4793             ;; Lines.
4794             (progn
4795               (goto-char p)
4796               (if (search-forward "\nlines: " nil t)
4797                   (if (numberp (setq lines (ignore-errors (read cur))))
4798                       lines 0)
4799                 0))
4800             ;; Xref.
4801             (progn
4802               (goto-char p)
4803               (and (search-forward "\nxref: " nil t)
4804                    (nnheader-header-value)))
4805             ;; Extra.
4806             (when gnus-extra-headers
4807               (let ((extra gnus-extra-headers)
4808                     out)
4809                 (while extra
4810                   (goto-char p)
4811                   (when (search-forward
4812                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4813                     (push (cons (car extra) (nnheader-header-value))
4814                           out))
4815                   (pop extra))
4816                 out))))
4817           (when (equal id ref)
4818             (setq ref nil))
4819
4820           (when gnus-alter-header-function
4821             (funcall gnus-alter-header-function header)
4822             (setq id (mail-header-id header)
4823                   ref (gnus-parent-id (mail-header-references header))))
4824
4825           (when (setq header
4826                       (gnus-dependencies-add-header
4827                        header dependencies force-new))
4828             (push header headers))
4829           (goto-char (point-max))
4830           (widen))
4831         (nreverse headers)))))
4832
4833 ;; Goes through the xover lines and returns a list of vectors
4834 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4835                                                   force-new dependencies
4836                                                   group also-fetch-heads)
4837   "Parse the news overview data in the server buffer.
4838 Return a list of headers that match SEQUENCE (see
4839 `nntp-retrieve-headers')."
4840   ;; Get the Xref when the users reads the articles since most/some
4841   ;; NNTP servers do not include Xrefs when using XOVER.
4842   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4843   (let ((mail-parse-charset gnus-newsgroup-charset)
4844         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4845         (cur nntp-server-buffer)
4846         (dependencies (or dependencies gnus-newsgroup-dependencies))
4847         number headers header)
4848     (save-excursion
4849       (set-buffer nntp-server-buffer)
4850       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4851       ;; Allow the user to mangle the headers before parsing them.
4852       (gnus-run-hooks 'gnus-parse-headers-hook)
4853       (goto-char (point-min))
4854       (while (not (eobp))
4855         (condition-case ()
4856             (while (and sequence (not (eobp)))
4857               (setq number (read cur))
4858               (while (and sequence
4859                           (< (car sequence) number))
4860                 (setq sequence (cdr sequence)))
4861               (and sequence
4862                    (eq number (car sequence))
4863                    (progn
4864                      (setq sequence (cdr sequence))
4865                      (setq header (inline
4866                                     (gnus-nov-parse-line
4867                                      number dependencies force-new))))
4868                    (push header headers))
4869               (forward-line 1))
4870           (error
4871            (gnus-error 4 "Strange nov line (%d)"
4872                        (count-lines (point-min) (point)))))
4873         (forward-line 1))
4874       ;; A common bug in inn is that if you have posted an article and
4875       ;; then retrieves the active file, it will answer correctly --
4876       ;; the new article is included.  However, a NOV entry for the
4877       ;; article may not have been generated yet, so this may fail.
4878       ;; We work around this problem by retrieving the last few
4879       ;; headers using HEAD.
4880       (if (or (not also-fetch-heads)
4881               (not sequence))
4882           ;; We (probably) got all the headers.
4883           (nreverse headers)
4884         (let ((gnus-nov-is-evil t))
4885           (nconc
4886            (nreverse headers)
4887            (when (gnus-retrieve-headers sequence group)
4888              (gnus-get-newsgroup-headers))))))))
4889
4890 (defun gnus-article-get-xrefs ()
4891   "Fill in the Xref value in `gnus-current-headers', if necessary.
4892 This is meant to be called in `gnus-article-internal-prepare-hook'."
4893   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4894                                  gnus-current-headers)))
4895     (or (not gnus-use-cross-reference)
4896         (not headers)
4897         (and (mail-header-xref headers)
4898              (not (string= (mail-header-xref headers) "")))
4899         (let ((case-fold-search t)
4900               xref)
4901           (save-restriction
4902             (nnheader-narrow-to-headers)
4903             (goto-char (point-min))
4904             (when (or (and (not (eobp))
4905                            (eq (downcase (char-after)) ?x)
4906                            (looking-at "Xref:"))
4907                       (search-forward "\nXref:" nil t))
4908               (goto-char (1+ (match-end 0)))
4909               (setq xref (buffer-substring (point)
4910                                            (progn (end-of-line) (point))))
4911               (mail-header-set-xref headers xref)))))))
4912
4913 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4914   "Find article ID and insert the summary line for that article.
4915 OLD-HEADER can either be a header or a line number to insert
4916 the subject line on."
4917   (let* ((line (and (numberp old-header) old-header))
4918          (old-header (and (vectorp old-header) old-header))
4919          (header (cond ((and old-header use-old-header)
4920                         old-header)
4921                        ((and (numberp id)
4922                              (gnus-number-to-header id))
4923                         (gnus-number-to-header id))
4924                        (t
4925                         (gnus-read-header id))))
4926          (number (and (numberp id) id))
4927          d)
4928     (when header
4929       ;; Rebuild the thread that this article is part of and go to the
4930       ;; article we have fetched.
4931       (when (and (not gnus-show-threads)
4932                  old-header)
4933         (when (and number
4934                    (setq d (gnus-data-find (mail-header-number old-header))))
4935           (goto-char (gnus-data-pos d))
4936           (gnus-data-remove
4937            number
4938            (- (gnus-point-at-bol)
4939               (prog1
4940                   (1+ (gnus-point-at-eol))
4941                 (gnus-delete-line))))))
4942       (when old-header
4943         (mail-header-set-number header (mail-header-number old-header)))
4944       (setq gnus-newsgroup-sparse
4945             (delq (setq number (mail-header-number header))
4946                   gnus-newsgroup-sparse))
4947       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4948       (push number gnus-newsgroup-limit)
4949       (gnus-rebuild-thread (mail-header-id header) line)
4950       (gnus-summary-goto-subject number nil t))
4951     (when (and (numberp number)
4952                (> number 0))
4953       ;; We have to update the boundaries even if we can't fetch the
4954       ;; article if ID is a number -- so that the next `P' or `N'
4955       ;; command will fetch the previous (or next) article even
4956       ;; if the one we tried to fetch this time has been canceled.
4957       (when (> number gnus-newsgroup-end)
4958         (setq gnus-newsgroup-end number))
4959       (when (< number gnus-newsgroup-begin)
4960         (setq gnus-newsgroup-begin number))
4961       (setq gnus-newsgroup-unselected
4962             (delq number gnus-newsgroup-unselected)))
4963     ;; Report back a success?
4964     (and header (mail-header-number header))))
4965
4966 ;;; Process/prefix in the summary buffer
4967
4968 (defun gnus-summary-work-articles (n)
4969   "Return a list of articles to be worked upon.
4970 The prefix argument, the list of process marked articles, and the
4971 current article will be taken into consideration."
4972   (save-excursion
4973     (set-buffer gnus-summary-buffer)
4974     (cond
4975      (n
4976       ;; A numerical prefix has been given.
4977       (setq n (prefix-numeric-value n))
4978       (let ((backward (< n 0))
4979             (n (abs (prefix-numeric-value n)))
4980             articles article)
4981         (save-excursion
4982           (while
4983               (and (> n 0)
4984                    (push (setq article (gnus-summary-article-number))
4985                          articles)
4986                    (if backward
4987                        (gnus-summary-find-prev nil article)
4988                      (gnus-summary-find-next nil article)))
4989             (decf n)))
4990         (nreverse articles)))
4991      ((and (gnus-region-active-p) (mark))
4992       (message "region active")
4993       ;; Work on the region between point and mark.
4994       (let ((max (max (point) (mark)))
4995             articles article)
4996         (save-excursion
4997           (goto-char (min (min (point) (mark))))
4998           (while
4999               (and
5000                (push (setq article (gnus-summary-article-number)) articles)
5001                (gnus-summary-find-next nil article)
5002                (< (point) max)))
5003           (nreverse articles))))
5004      (gnus-newsgroup-processable
5005       ;; There are process-marked articles present.
5006       ;; Save current state.
5007       (gnus-summary-save-process-mark)
5008       ;; Return the list.
5009       (reverse gnus-newsgroup-processable))
5010      (t
5011       ;; Just return the current article.
5012       (list (gnus-summary-article-number))))))
5013
5014 (defmacro gnus-summary-iterate (arg &rest forms)
5015   "Iterate over the process/prefixed articles and do FORMS.
5016 ARG is the interactive prefix given to the command.  FORMS will be
5017 executed with point over the summary line of the articles."
5018   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5019     `(let ((,articles (gnus-summary-work-articles ,arg)))
5020        (while ,articles
5021          (gnus-summary-goto-subject (car ,articles))
5022          ,@forms
5023          (pop ,articles)))))
5024
5025 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5026 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5027
5028 (defun gnus-summary-save-process-mark ()
5029   "Push the current set of process marked articles on the stack."
5030   (interactive)
5031   (push (copy-sequence gnus-newsgroup-processable)
5032         gnus-newsgroup-process-stack))
5033
5034 (defun gnus-summary-kill-process-mark ()
5035   "Push the current set of process marked articles on the stack and unmark."
5036   (interactive)
5037   (gnus-summary-save-process-mark)
5038   (gnus-summary-unmark-all-processable))
5039
5040 (defun gnus-summary-yank-process-mark ()
5041   "Pop the last process mark state off the stack and restore it."
5042   (interactive)
5043   (unless gnus-newsgroup-process-stack
5044     (error "Empty mark stack"))
5045   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5046
5047 (defun gnus-summary-process-mark-set (set)
5048   "Make SET into the current process marked articles."
5049   (gnus-summary-unmark-all-processable)
5050   (while set
5051     (gnus-summary-set-process-mark (pop set))))
5052
5053 ;;; Searching and stuff
5054
5055 (defun gnus-summary-search-group (&optional backward use-level)
5056   "Search for next unread newsgroup.
5057 If optional argument BACKWARD is non-nil, search backward instead."
5058   (save-excursion
5059     (set-buffer gnus-group-buffer)
5060     (when (gnus-group-search-forward
5061            backward nil (if use-level (gnus-group-group-level) nil))
5062       (gnus-group-group-name))))
5063
5064 (defun gnus-summary-best-group (&optional exclude-group)
5065   "Find the name of the best unread group.
5066 If EXCLUDE-GROUP, do not go to this group."
5067   (save-excursion
5068     (set-buffer gnus-group-buffer)
5069     (save-excursion
5070       (gnus-group-best-unread-group exclude-group))))
5071
5072 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5073   (if backward (gnus-summary-find-prev)
5074     (let* ((dummy (gnus-summary-article-intangible-p))
5075            (article (or article (gnus-summary-article-number)))
5076            (arts (gnus-data-find-list article))
5077            result)
5078       (when (and (not dummy)
5079                  (or (not gnus-summary-check-current)
5080                      (not unread)
5081                      (not (gnus-data-unread-p (car arts)))))
5082         (setq arts (cdr arts)))
5083       (when (setq result
5084                   (if unread
5085                       (progn
5086                         (while arts
5087                           (when (or (and undownloaded
5088                                          (eq gnus-undownloaded-mark
5089                                              (gnus-data-mark (car arts))))
5090                                     (gnus-data-unread-p (car arts)))
5091                             (setq result (car arts)
5092                                   arts nil))
5093                           (setq arts (cdr arts)))
5094                         result)
5095                     (car arts)))
5096         (goto-char (gnus-data-pos result))
5097         (gnus-data-number result)))))
5098
5099 (defun gnus-summary-find-prev (&optional unread article)
5100   (let* ((eobp (eobp))
5101          (article (or article (gnus-summary-article-number)))
5102          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5103          result)
5104     (when (and (not eobp)
5105                (or (not gnus-summary-check-current)
5106                    (not unread)
5107                    (not (gnus-data-unread-p (car arts)))))
5108       (setq arts (cdr arts)))
5109     (when (setq result
5110                 (if unread
5111                     (progn
5112                       (while arts
5113                         (when (gnus-data-unread-p (car arts))
5114                           (setq result (car arts)
5115                                 arts nil))
5116                         (setq arts (cdr arts)))
5117                       result)
5118                   (car arts)))
5119       (goto-char (gnus-data-pos result))
5120       (gnus-data-number result))))
5121
5122 (defun gnus-summary-find-subject (subject &optional unread backward article)
5123   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5124          (article (or article (gnus-summary-article-number)))
5125          (articles (gnus-data-list backward))
5126          (arts (gnus-data-find-list article articles))
5127          result)
5128     (when (or (not gnus-summary-check-current)
5129               (not unread)
5130               (not (gnus-data-unread-p (car arts))))
5131       (setq arts (cdr arts)))
5132     (while arts
5133       (and (or (not unread)
5134                (gnus-data-unread-p (car arts)))
5135            (vectorp (gnus-data-header (car arts)))
5136            (gnus-subject-equal
5137             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5138            (setq result (car arts)
5139                  arts nil))
5140       (setq arts (cdr arts)))
5141     (and result
5142          (goto-char (gnus-data-pos result))
5143          (gnus-data-number result))))
5144
5145 (defun gnus-summary-search-forward (&optional unread subject backward)
5146   "Search forward for an article.
5147 If UNREAD, look for unread articles.  If SUBJECT, look for
5148 articles with that subject.  If BACKWARD, search backward instead."
5149   (cond (subject (gnus-summary-find-subject subject unread backward))
5150         (backward (gnus-summary-find-prev unread))
5151         (t (gnus-summary-find-next unread))))
5152
5153 (defun gnus-recenter (&optional n)
5154   "Center point in window and redisplay frame.
5155 Also do horizontal recentering."
5156   (interactive "P")
5157   (when (and gnus-auto-center-summary
5158              (not (eq gnus-auto-center-summary 'vertical)))
5159     (gnus-horizontal-recenter))
5160   (recenter n))
5161
5162 (defun gnus-summary-recenter ()
5163   "Center point in the summary window.
5164 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5165 displayed, no centering will be performed."
5166   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5167   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5168   (interactive)
5169   (let* ((top (cond ((< (window-height) 4) 0)
5170                     ((< (window-height) 7) 1)
5171                     (t (if (numberp gnus-auto-center-summary)
5172                            gnus-auto-center-summary
5173                          2))))
5174          (height (1- (window-height)))
5175          (bottom (save-excursion (goto-char (point-max))
5176                                  (forward-line (- height))
5177                                  (point)))
5178          (window (get-buffer-window (current-buffer))))
5179     ;; The user has to want it.
5180     (when gnus-auto-center-summary
5181       (when (get-buffer-window gnus-article-buffer)
5182         ;; Only do recentering when the article buffer is displayed,
5183         ;; Set the window start to either `bottom', which is the biggest
5184         ;; possible valid number, or the second line from the top,
5185         ;; whichever is the least.
5186         (set-window-start
5187          window (min bottom (save-excursion
5188                               (forward-line (- top)) (point)))
5189          t))
5190       ;; Do horizontal recentering while we're at it.
5191       (when (and (get-buffer-window (current-buffer) t)
5192                  (not (eq gnus-auto-center-summary 'vertical)))
5193         (let ((selected (selected-window)))
5194           (select-window (get-buffer-window (current-buffer) t))
5195           (gnus-summary-position-point)
5196           (gnus-horizontal-recenter)
5197           (select-window selected))))))
5198
5199 (defun gnus-summary-jump-to-group (newsgroup)
5200   "Move point to NEWSGROUP in group mode buffer."
5201   ;; Keep update point of group mode buffer if visible.
5202   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5203       (save-window-excursion
5204         ;; Take care of tree window mode.
5205         (when (get-buffer-window gnus-group-buffer)
5206           (pop-to-buffer gnus-group-buffer))
5207         (gnus-group-jump-to-group newsgroup))
5208     (save-excursion
5209       ;; Take care of tree window mode.
5210       (if (get-buffer-window gnus-group-buffer)
5211           (pop-to-buffer gnus-group-buffer)
5212         (set-buffer gnus-group-buffer))
5213       (gnus-group-jump-to-group newsgroup))))
5214
5215 ;; This function returns a list of article numbers based on the
5216 ;; difference between the ranges of read articles in this group and
5217 ;; the range of active articles.
5218 (defun gnus-list-of-unread-articles (group)
5219   (let* ((read (gnus-info-read (gnus-get-info group)))
5220          (active (or (gnus-active group) (gnus-activate-group group)))
5221          (last (cdr active))
5222          first nlast unread)
5223     ;; If none are read, then all are unread.
5224     (if (not read)
5225         (setq first (car active))
5226       ;; If the range of read articles is a single range, then the
5227       ;; first unread article is the article after the last read
5228       ;; article.  Sounds logical, doesn't it?
5229       (if (and (not (listp (cdr read)))
5230                (or (< (car read) (car active))
5231                    (progn (setq read (list read))
5232                           nil)))
5233           (setq first (max (car active) (1+ (cdr read))))
5234         ;; `read' is a list of ranges.
5235         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5236                                   (caar read)))
5237                   1)
5238           (setq first (car active)))
5239         (while read
5240           (when first
5241             (while (< first nlast)
5242               (push first unread)
5243               (setq first (1+ first))))
5244           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5245           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5246           (setq read (cdr read)))))
5247     ;; And add the last unread articles.
5248     (while (<= first last)
5249       (push first unread)
5250       (setq first (1+ first)))
5251     ;; Return the list of unread articles.
5252     (delq 0 (nreverse unread))))
5253
5254 (defun gnus-list-of-read-articles (group)
5255   "Return a list of unread, unticked and non-dormant articles."
5256   (let* ((info (gnus-get-info group))
5257          (marked (gnus-info-marks info))
5258          (active (gnus-active group)))
5259     (and info active
5260          (gnus-set-difference
5261           (gnus-sorted-complement
5262            (gnus-uncompress-range active)
5263            (gnus-list-of-unread-articles group))
5264           (append
5265            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5266            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5267
5268 ;; Various summary commands
5269
5270 (defun gnus-summary-select-article-buffer ()
5271   "Reconfigure windows to show article buffer."
5272   (interactive)
5273   (if (not (gnus-buffer-live-p gnus-article-buffer))
5274       (error "There is no article buffer for this summary buffer")
5275     (gnus-configure-windows 'article)
5276     (select-window (get-buffer-window gnus-article-buffer))))
5277
5278 (defun gnus-summary-universal-argument (arg)
5279   "Perform any operation on all articles that are process/prefixed."
5280   (interactive "P")
5281   (let ((articles (gnus-summary-work-articles arg))
5282         func article)
5283     (if (eq
5284          (setq
5285           func
5286           (key-binding
5287            (read-key-sequence
5288             (substitute-command-keys
5289              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5290          'undefined)
5291         (gnus-error 1 "Undefined key")
5292       (save-excursion
5293         (while articles
5294           (gnus-summary-goto-subject (setq article (pop articles)))
5295           (let (gnus-newsgroup-processable)
5296             (command-execute func))
5297           (gnus-summary-remove-process-mark article)))))
5298   (gnus-summary-position-point))
5299
5300 (defun gnus-summary-toggle-truncation (&optional arg)
5301   "Toggle truncation of summary lines.
5302 With arg, turn line truncation on iff arg is positive."
5303   (interactive "P")
5304   (setq truncate-lines
5305         (if (null arg) (not truncate-lines)
5306           (> (prefix-numeric-value arg) 0)))
5307   (redraw-display))
5308
5309 (defun gnus-summary-reselect-current-group (&optional all rescan)
5310   "Exit and then reselect the current newsgroup.
5311 The prefix argument ALL means to select all articles."
5312   (interactive "P")
5313   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5314     (error "Ephemeral groups can't be reselected"))
5315   (let ((current-subject (gnus-summary-article-number))
5316         (group gnus-newsgroup-name))
5317     (setq gnus-newsgroup-begin nil)
5318     (gnus-summary-exit)
5319     ;; We have to adjust the point of group mode buffer because
5320     ;; point was moved to the next unread newsgroup by exiting.
5321     (gnus-summary-jump-to-group group)
5322     (when rescan
5323       (save-excursion
5324         (gnus-group-get-new-news-this-group 1)))
5325     (gnus-group-read-group all t)
5326     (gnus-summary-goto-subject current-subject nil t)))
5327
5328 (defun gnus-summary-rescan-group (&optional all)
5329   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5330   (interactive "P")
5331   (gnus-summary-reselect-current-group all t))
5332
5333 (defun gnus-summary-update-info (&optional non-destructive)
5334   (save-excursion
5335     (let ((group gnus-newsgroup-name))
5336       (when group
5337         (when gnus-newsgroup-kill-headers
5338           (setq gnus-newsgroup-killed
5339                 (gnus-compress-sequence
5340                  (nconc
5341                   (gnus-set-sorted-intersection
5342                    (gnus-uncompress-range gnus-newsgroup-killed)
5343                    (setq gnus-newsgroup-unselected
5344                          (sort gnus-newsgroup-unselected '<)))
5345                   (setq gnus-newsgroup-unreads
5346                         (sort gnus-newsgroup-unreads '<)))
5347                  t)))
5348         (unless (listp (cdr gnus-newsgroup-killed))
5349           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5350         (let ((headers gnus-newsgroup-headers))
5351           ;; Set the new ranges of read articles.
5352           (save-excursion
5353             (set-buffer gnus-group-buffer)
5354             (gnus-undo-force-boundary))
5355           (gnus-update-read-articles
5356            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5357           ;; Set the current article marks.
5358           (let ((gnus-newsgroup-scored
5359                  (if (and (not gnus-save-score)
5360                           (not non-destructive))
5361                      nil
5362                    gnus-newsgroup-scored)))
5363             (save-excursion
5364               (gnus-update-marks)))
5365           ;; Do the cross-ref thing.
5366           (when gnus-use-cross-reference
5367             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5368           ;; Do not switch windows but change the buffer to work.
5369           (set-buffer gnus-group-buffer)
5370           (unless (gnus-ephemeral-group-p group)
5371             (gnus-group-update-group group)))))))
5372
5373 (defun gnus-summary-save-newsrc (&optional force)
5374   "Save the current number of read/marked articles in the dribble buffer.
5375 The dribble buffer will then be saved.
5376 If FORCE (the prefix), also save the .newsrc file(s)."
5377   (interactive "P")
5378   (gnus-summary-update-info t)
5379   (if force
5380       (gnus-save-newsrc-file)
5381     (gnus-dribble-save)))
5382
5383 (defun gnus-summary-exit (&optional temporary)
5384   "Exit reading current newsgroup, and then return to group selection mode.
5385 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5386   (interactive)
5387   (gnus-set-global-variables)
5388   (when (gnus-buffer-live-p gnus-article-buffer)
5389     (save-excursion
5390       (set-buffer gnus-article-buffer)
5391       (mm-destroy-parts gnus-article-mime-handles)
5392       (setq gnus-article-mime-handles nil)))
5393   (gnus-kill-save-kill-buffer)
5394   (gnus-async-halt-prefetch)
5395   (let* ((group gnus-newsgroup-name)
5396          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5397          (mode major-mode)
5398          (group-point nil)
5399          (buf (current-buffer)))
5400     (unless quit-config
5401       ;; Do adaptive scoring, and possibly save score files.
5402       (when gnus-newsgroup-adaptive
5403         (gnus-score-adaptive))
5404       (when gnus-use-scoring
5405         (gnus-score-save)))
5406     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5407     ;; If we have several article buffers, we kill them at exit.
5408     (unless gnus-single-article-buffer
5409       (gnus-kill-buffer gnus-original-article-buffer)
5410       (setq gnus-article-current nil))
5411     (when gnus-use-cache
5412       (gnus-cache-possibly-remove-articles)
5413       (gnus-cache-save-buffers))
5414     (gnus-async-prefetch-remove-group group)
5415     (when gnus-suppress-duplicates
5416       (gnus-dup-enter-articles))
5417     (when gnus-use-trees
5418       (gnus-tree-close group))
5419     (when gnus-use-cache
5420       (gnus-cache-write-active))
5421     ;; Remove entries for this group.
5422     (nnmail-purge-split-history (gnus-group-real-name group))
5423     ;; Make all changes in this group permanent.
5424     (unless quit-config
5425       (gnus-run-hooks 'gnus-exit-group-hook)
5426       (gnus-summary-update-info))
5427     (gnus-close-group group)
5428     ;; Make sure where we were, and go to next newsgroup.
5429     (set-buffer gnus-group-buffer)
5430     (unless quit-config
5431       (gnus-group-jump-to-group group))
5432     (gnus-run-hooks 'gnus-summary-exit-hook)
5433     (unless (or quit-config
5434                 ;; If this group has disappeared from the summary
5435                 ;; buffer, don't skip forwards.
5436                 (not (string= group (gnus-group-group-name))))
5437       (gnus-group-next-unread-group 1))
5438     (setq group-point (point))
5439     (if temporary
5440         nil                             ;Nothing to do.
5441       ;; If we have several article buffers, we kill them at exit.
5442       (unless gnus-single-article-buffer
5443         (gnus-kill-buffer gnus-article-buffer)
5444         (gnus-kill-buffer gnus-original-article-buffer)
5445         (setq gnus-article-current nil))
5446       (set-buffer buf)
5447       (if (not gnus-kill-summary-on-exit)
5448           (gnus-deaden-summary)
5449         ;; We set all buffer-local variables to nil.  It is unclear why
5450         ;; this is needed, but if we don't, buffer-local variables are
5451         ;; not garbage-collected, it seems.  This would the lead to en
5452         ;; ever-growing Emacs.
5453         (gnus-summary-clear-local-variables)
5454         (when (get-buffer gnus-article-buffer)
5455           (bury-buffer gnus-article-buffer))
5456         ;; We clear the global counterparts of the buffer-local
5457         ;; variables as well, just to be on the safe side.
5458         (set-buffer gnus-group-buffer)
5459         (gnus-summary-clear-local-variables)
5460         ;; Return to group mode buffer.
5461         (when (eq mode 'gnus-summary-mode)
5462           (gnus-kill-buffer buf)))
5463       (setq gnus-current-select-method gnus-select-method)
5464       (pop-to-buffer gnus-group-buffer)
5465       (if (not quit-config)
5466           (progn
5467             (goto-char group-point)
5468             (gnus-configure-windows 'group 'force))
5469         (gnus-handle-ephemeral-exit quit-config))
5470       ;; Clear the current group name.
5471       (unless quit-config
5472         (setq gnus-newsgroup-name nil)))))
5473
5474 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5475 (defun gnus-summary-exit-no-update (&optional no-questions)
5476   "Quit reading current newsgroup without updating read article info."
5477   (interactive)
5478   (let* ((group gnus-newsgroup-name)
5479          (quit-config (gnus-group-quit-config group)))
5480     (when (or no-questions
5481               gnus-expert-user
5482               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5483       (gnus-async-halt-prefetch)
5484       (mapcar 'funcall
5485               (delq 'gnus-summary-expire-articles
5486                     (copy-sequence gnus-summary-prepare-exit-hook)))
5487       (when (gnus-buffer-live-p gnus-article-buffer)
5488         (save-excursion
5489           (set-buffer gnus-article-buffer)
5490           (mm-destroy-parts gnus-article-mime-handles)
5491           (setq gnus-article-mime-handles nil)))
5492       ;; If we have several article buffers, we kill them at exit.
5493       (unless gnus-single-article-buffer
5494         (gnus-kill-buffer gnus-article-buffer)
5495         (gnus-kill-buffer gnus-original-article-buffer)
5496         (setq gnus-article-current nil))
5497       (if (not gnus-kill-summary-on-exit)
5498           (gnus-deaden-summary)
5499         (gnus-close-group group)
5500         (gnus-summary-clear-local-variables)
5501         (set-buffer gnus-group-buffer)
5502         (gnus-summary-clear-local-variables)
5503         (when (get-buffer gnus-summary-buffer)
5504           (kill-buffer gnus-summary-buffer)))
5505       (unless gnus-single-article-buffer
5506         (setq gnus-article-current nil))
5507       (when gnus-use-trees
5508         (gnus-tree-close group))
5509       (gnus-async-prefetch-remove-group group)
5510       (when (get-buffer gnus-article-buffer)
5511         (bury-buffer gnus-article-buffer))
5512       ;; Return to the group buffer.
5513       (gnus-configure-windows 'group 'force)
5514       ;; Clear the current group name.
5515       (setq gnus-newsgroup-name nil)
5516       (when (equal (gnus-group-group-name) group)
5517         (gnus-group-next-unread-group 1))
5518       (when quit-config
5519         (gnus-handle-ephemeral-exit quit-config)))))
5520
5521 (defun gnus-handle-ephemeral-exit (quit-config)
5522   "Handle movement when leaving an ephemeral group.
5523 The state which existed when entering the ephemeral is reset."
5524   (if (not (buffer-name (car quit-config)))
5525       (gnus-configure-windows 'group 'force)
5526     (set-buffer (car quit-config))
5527     (cond ((eq major-mode 'gnus-summary-mode)
5528            (gnus-set-global-variables))
5529           ((eq major-mode 'gnus-article-mode)
5530            (save-excursion
5531              ;; The `gnus-summary-buffer' variable may point
5532              ;; to the old summary buffer when using a single
5533              ;; article buffer.
5534              (unless (gnus-buffer-live-p gnus-summary-buffer)
5535                (set-buffer gnus-group-buffer))
5536              (set-buffer gnus-summary-buffer)
5537              (gnus-set-global-variables))))
5538     (if (or (eq (cdr quit-config) 'article)
5539             (eq (cdr quit-config) 'pick))
5540         (progn
5541           ;; The current article may be from the ephemeral group
5542           ;; thus it is best that we reload this article
5543           (gnus-summary-show-article)
5544           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5545               (gnus-configure-windows 'pick 'force)
5546             (gnus-configure-windows (cdr quit-config) 'force)))
5547       (gnus-configure-windows (cdr quit-config) 'force))
5548     (when (eq major-mode 'gnus-summary-mode)
5549       (gnus-summary-next-subject 1 nil t)
5550       (gnus-summary-recenter)
5551       (gnus-summary-position-point))))
5552
5553 ;;; Dead summaries.
5554
5555 (defvar gnus-dead-summary-mode-map nil)
5556
5557 (unless gnus-dead-summary-mode-map
5558   (setq gnus-dead-summary-mode-map (make-keymap))
5559   (suppress-keymap gnus-dead-summary-mode-map)
5560   (substitute-key-definition
5561    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5562   (let ((keys '("\C-d" "\r" "\177" [delete])))
5563     (while keys
5564       (define-key gnus-dead-summary-mode-map
5565         (pop keys) 'gnus-summary-wake-up-the-dead))))
5566
5567 (defvar gnus-dead-summary-mode nil
5568   "Minor mode for Gnus summary buffers.")
5569
5570 (defun gnus-dead-summary-mode (&optional arg)
5571   "Minor mode for Gnus summary buffers."
5572   (interactive "P")
5573   (when (eq major-mode 'gnus-summary-mode)
5574     (make-local-variable 'gnus-dead-summary-mode)
5575     (setq gnus-dead-summary-mode
5576           (if (null arg) (not gnus-dead-summary-mode)
5577             (> (prefix-numeric-value arg) 0)))
5578     (when gnus-dead-summary-mode
5579       (gnus-add-minor-mode
5580        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5581
5582 (defun gnus-deaden-summary ()
5583   "Make the current summary buffer into a dead summary buffer."
5584   ;; Kill any previous dead summary buffer.
5585   (when (and gnus-dead-summary
5586              (buffer-name gnus-dead-summary))
5587     (save-excursion
5588       (set-buffer gnus-dead-summary)
5589       (when gnus-dead-summary-mode
5590         (kill-buffer (current-buffer)))))
5591   ;; Make this the current dead summary.
5592   (setq gnus-dead-summary (current-buffer))
5593   (gnus-dead-summary-mode 1)
5594   (let ((name (buffer-name)))
5595     (when (string-match "Summary" name)
5596       (rename-buffer
5597        (concat (substring name 0 (match-beginning 0)) "Dead "
5598                (substring name (match-beginning 0)))
5599        t)
5600       (bury-buffer))))
5601
5602 (defun gnus-kill-or-deaden-summary (buffer)
5603   "Kill or deaden the summary BUFFER."
5604   (save-excursion
5605     (when (and (buffer-name buffer)
5606                (not gnus-single-article-buffer))
5607       (save-excursion
5608         (set-buffer buffer)
5609         (gnus-kill-buffer gnus-article-buffer)
5610         (gnus-kill-buffer gnus-original-article-buffer)))
5611     (cond (gnus-kill-summary-on-exit
5612            (when (and gnus-use-trees
5613                       (gnus-buffer-exists-p buffer))
5614              (save-excursion
5615                (set-buffer buffer)
5616                (gnus-tree-close gnus-newsgroup-name)))
5617            (gnus-kill-buffer buffer))
5618           ((gnus-buffer-exists-p buffer)
5619            (save-excursion
5620              (set-buffer buffer)
5621              (gnus-deaden-summary))))))
5622
5623 (defun gnus-summary-wake-up-the-dead (&rest args)
5624   "Wake up the dead summary buffer."
5625   (interactive)
5626   (gnus-dead-summary-mode -1)
5627   (let ((name (buffer-name)))
5628     (when (string-match "Dead " name)
5629       (rename-buffer
5630        (concat (substring name 0 (match-beginning 0))
5631                (substring name (match-end 0)))
5632        t)))
5633   (gnus-message 3 "This dead summary is now alive again"))
5634
5635 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5636 (defun gnus-summary-fetch-faq (&optional faq-dir)
5637   "Fetch the FAQ for the current group.
5638 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5639 in."
5640   (interactive
5641    (list
5642     (when current-prefix-arg
5643       (completing-read
5644        "Faq dir: " (and (listp gnus-group-faq-directory)
5645                         (mapcar (lambda (file) (list file))
5646                                 gnus-group-faq-directory))))))
5647   (let (gnus-faq-buffer)
5648     (when (setq gnus-faq-buffer
5649                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5650       (gnus-configure-windows 'summary-faq))))
5651
5652 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5653 (defun gnus-summary-describe-group (&optional force)
5654   "Describe the current newsgroup."
5655   (interactive "P")
5656   (gnus-group-describe-group force gnus-newsgroup-name))
5657
5658 (defun gnus-summary-describe-briefly ()
5659   "Describe summary mode commands briefly."
5660   (interactive)
5661   (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")))
5662
5663 ;; Walking around group mode buffer from summary mode.
5664
5665 (defun gnus-summary-next-group (&optional no-article target-group backward)
5666   "Exit current newsgroup and then select next unread newsgroup.
5667 If prefix argument NO-ARTICLE is non-nil, no article is selected
5668 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5669 previous group instead."
5670   (interactive "P")
5671   ;; Stop pre-fetching.
5672   (gnus-async-halt-prefetch)
5673   (let ((current-group gnus-newsgroup-name)
5674         (current-buffer (current-buffer))
5675         entered)
5676     ;; First we semi-exit this group to update Xrefs and all variables.
5677     ;; We can't do a real exit, because the window conf must remain
5678     ;; the same in case the user is prompted for info, and we don't
5679     ;; want the window conf to change before that...
5680     (gnus-summary-exit t)
5681     (while (not entered)
5682       ;; Then we find what group we are supposed to enter.
5683       (set-buffer gnus-group-buffer)
5684       (gnus-group-jump-to-group current-group)
5685       (setq target-group
5686             (or target-group
5687                 (if (eq gnus-keep-same-level 'best)
5688                     (gnus-summary-best-group gnus-newsgroup-name)
5689                   (gnus-summary-search-group backward gnus-keep-same-level))))
5690       (if (not target-group)
5691           ;; There are no further groups, so we return to the group
5692           ;; buffer.
5693           (progn
5694             (gnus-message 5 "Returning to the group buffer")
5695             (setq entered t)
5696             (when (gnus-buffer-live-p current-buffer)
5697               (set-buffer current-buffer)
5698               (gnus-summary-exit))
5699             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5700         ;; We try to enter the target group.
5701         (gnus-group-jump-to-group target-group)
5702         (let ((unreads (gnus-group-group-unread)))
5703           (if (and (or (eq t unreads)
5704                        (and unreads (not (zerop unreads))))
5705                    (gnus-summary-read-group
5706                     target-group nil no-article
5707                     (and (buffer-name current-buffer) current-buffer)
5708                     nil backward))
5709               (setq entered t)
5710             (setq current-group target-group
5711                   target-group nil)))))))
5712
5713 (defun gnus-summary-prev-group (&optional no-article)
5714   "Exit current newsgroup and then select previous unread newsgroup.
5715 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5716   (interactive "P")
5717   (gnus-summary-next-group no-article nil t))
5718
5719 ;; Walking around summary lines.
5720
5721 (defun gnus-summary-first-subject (&optional unread undownloaded)
5722   "Go to the first unread subject.
5723 If UNREAD is non-nil, go to the first unread article.
5724 Returns the article selected or nil if there are no unread articles."
5725   (interactive "P")
5726   (prog1
5727       (cond
5728        ;; Empty summary.
5729        ((null gnus-newsgroup-data)
5730         (gnus-message 3 "No articles in the group")
5731         nil)
5732        ;; Pick the first article.
5733        ((not unread)
5734         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5735         (gnus-data-number (car gnus-newsgroup-data)))
5736        ;; No unread articles.
5737        ((null gnus-newsgroup-unreads)
5738         (gnus-message 3 "No more unread articles")
5739         nil)
5740        ;; Find the first unread article.
5741        (t
5742         (let ((data gnus-newsgroup-data))
5743           (while (and data
5744                       (and (not (and undownloaded
5745                                      (eq gnus-undownloaded-mark
5746                                          (gnus-data-mark (car data)))))
5747                            (not (gnus-data-unread-p (car data)))))
5748             (setq data (cdr data)))
5749           (when data
5750             (goto-char (gnus-data-pos (car data)))
5751             (gnus-data-number (car data))))))
5752     (gnus-summary-position-point)))
5753
5754 (defun gnus-summary-next-subject (n &optional unread dont-display)
5755   "Go to next N'th summary line.
5756 If N is negative, go to the previous N'th subject line.
5757 If UNREAD is non-nil, only unread articles are selected.
5758 The difference between N and the actual number of steps taken is
5759 returned."
5760   (interactive "p")
5761   (let ((backward (< n 0))
5762         (n (abs n)))
5763     (while (and (> n 0)
5764                 (if backward
5765                     (gnus-summary-find-prev unread)
5766                   (gnus-summary-find-next unread)))
5767       (unless (zerop (setq n (1- n)))
5768         (gnus-summary-show-thread)))
5769     (when (/= 0 n)
5770       (gnus-message 7 "No more%s articles"
5771                     (if unread " unread" "")))
5772     (unless dont-display
5773       (gnus-summary-recenter)
5774       (gnus-summary-position-point))
5775     n))
5776
5777 (defun gnus-summary-next-unread-subject (n)
5778   "Go to next N'th unread summary line."
5779   (interactive "p")
5780   (gnus-summary-next-subject n t))
5781
5782 (defun gnus-summary-prev-subject (n &optional unread)
5783   "Go to previous N'th summary line.
5784 If optional argument UNREAD is non-nil, only unread article is selected."
5785   (interactive "p")
5786   (gnus-summary-next-subject (- n) unread))
5787
5788 (defun gnus-summary-prev-unread-subject (n)
5789   "Go to previous N'th unread summary line."
5790   (interactive "p")
5791   (gnus-summary-next-subject (- n) t))
5792
5793 (defun gnus-summary-goto-subject (article &optional force silent)
5794   "Go the subject line of ARTICLE.
5795 If FORCE, also allow jumping to articles not currently shown."
5796   (interactive "nArticle number: ")
5797   (let ((b (point))
5798         (data (gnus-data-find article)))
5799     ;; We read in the article if we have to.
5800     (and (not data)
5801          force
5802          (gnus-summary-insert-subject
5803           article
5804           (if (or (numberp force) (vectorp force)) force)
5805           t)
5806          (setq data (gnus-data-find article)))
5807     (goto-char b)
5808     (if (not data)
5809         (progn
5810           (unless silent
5811             (gnus-message 3 "Can't find article %d" article))
5812           nil)
5813       (goto-char (gnus-data-pos data))
5814       (gnus-summary-position-point)
5815       article)))
5816
5817 ;; Walking around summary lines with displaying articles.
5818
5819 (defun gnus-summary-expand-window (&optional arg)
5820   "Make the summary buffer take up the entire Emacs frame.
5821 Given a prefix, will force an `article' buffer configuration."
5822   (interactive "P")
5823   (if arg
5824       (gnus-configure-windows 'article 'force)
5825     (gnus-configure-windows 'summary 'force)))
5826
5827 (defun gnus-summary-display-article (article &optional all-header)
5828   "Display ARTICLE in article buffer."
5829   (gnus-set-global-variables)
5830   (if (null article)
5831       nil
5832     (prog1
5833         (if gnus-summary-display-article-function
5834             (funcall gnus-summary-display-article-function article all-header)
5835           (gnus-article-prepare article all-header))
5836       (gnus-run-hooks 'gnus-select-article-hook)
5837       (when (and gnus-current-article
5838                  (not (zerop gnus-current-article)))
5839         (gnus-summary-goto-subject gnus-current-article))
5840       (gnus-summary-recenter)
5841       (when (and gnus-use-trees gnus-show-threads)
5842         (gnus-possibly-generate-tree article)
5843         (gnus-highlight-selected-tree article))
5844       ;; Successfully display article.
5845       (gnus-article-set-window-start
5846        (cdr (assq article gnus-newsgroup-bookmarks))))))
5847
5848 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5849   "Select the current article.
5850 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5851 non-nil, the article will be re-fetched even if it already present in
5852 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5853 be displayed."
5854   ;; Make sure we are in the summary buffer to work around bbdb bug.
5855   (unless (eq major-mode 'gnus-summary-mode)
5856     (set-buffer gnus-summary-buffer))
5857   (let ((article (or article (gnus-summary-article-number)))
5858         (all-headers (not (not all-headers))) ;Must be T or NIL.
5859         gnus-summary-display-article-function)
5860     (and (not pseudo)
5861          (gnus-summary-article-pseudo-p article)
5862          (error "This is a pseudo-article"))
5863     (save-excursion
5864       (set-buffer gnus-summary-buffer)
5865       (if (or (and gnus-single-article-buffer
5866                    (or (null gnus-current-article)
5867                        (null gnus-article-current)
5868                        (null (get-buffer gnus-article-buffer))
5869                        (not (eq article (cdr gnus-article-current)))
5870                        (not (equal (car gnus-article-current)
5871                                    gnus-newsgroup-name))))
5872               (and (not gnus-single-article-buffer)
5873                    (or (null gnus-current-article)
5874                        (not (eq gnus-current-article article))))
5875               force)
5876           ;; The requested article is different from the current article.
5877           (progn
5878             (when (gnus-buffer-live-p gnus-article-buffer)
5879               (with-current-buffer gnus-article-buffer
5880                 (mm-enable-multibyte)))
5881             (gnus-summary-display-article article all-headers)
5882             (when (gnus-buffer-live-p gnus-article-buffer)
5883               (with-current-buffer gnus-article-buffer
5884                 (if (not gnus-article-decoded-p) ;; a local variable
5885                     (mm-disable-multibyte))))
5886             (when (or all-headers gnus-show-all-headers)
5887               (gnus-article-show-all-headers))
5888             (gnus-article-set-window-start
5889              (cdr (assq article gnus-newsgroup-bookmarks)))
5890             article)
5891         (when (or all-headers gnus-show-all-headers)
5892           (gnus-article-show-all-headers))
5893         'old))))
5894
5895 (defun gnus-summary-set-current-mark (&optional current-mark)
5896   "Obsolete function."
5897   nil)
5898
5899 (defun gnus-summary-next-article (&optional unread subject backward push)
5900   "Select the next article.
5901 If UNREAD, only unread articles are selected.
5902 If SUBJECT, only articles with SUBJECT are selected.
5903 If BACKWARD, the previous article is selected instead of the next."
5904   (interactive "P")
5905   (cond
5906    ;; Is there such an article?
5907    ((and (gnus-summary-search-forward unread subject backward)
5908          (or (gnus-summary-display-article (gnus-summary-article-number))
5909              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5910     (gnus-summary-position-point))
5911    ;; If not, we try the first unread, if that is wanted.
5912    ((and subject
5913          gnus-auto-select-same
5914          (gnus-summary-first-unread-article))
5915     (gnus-summary-position-point)
5916     (gnus-message 6 "Wrapped"))
5917    ;; Try to get next/previous article not displayed in this group.
5918    ((and gnus-auto-extend-newsgroup
5919          (not unread) (not subject))
5920     (gnus-summary-goto-article
5921      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5922      nil (count-lines (point-min) (point))))
5923    ;; Go to next/previous group.
5924    (t
5925     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5926       (gnus-summary-jump-to-group gnus-newsgroup-name))
5927     (let ((cmd last-command-char)
5928           (point
5929            (save-excursion
5930              (set-buffer gnus-group-buffer)
5931              (point)))
5932           (group
5933            (if (eq gnus-keep-same-level 'best)
5934                (gnus-summary-best-group gnus-newsgroup-name)
5935              (gnus-summary-search-group backward gnus-keep-same-level))))
5936       ;; For some reason, the group window gets selected.  We change
5937       ;; it back.
5938       (select-window (get-buffer-window (current-buffer)))
5939       ;; Select next unread newsgroup automagically.
5940       (cond
5941        ((or (not gnus-auto-select-next)
5942             (not cmd))
5943         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5944        ((or (eq gnus-auto-select-next 'quietly)
5945             (and (eq gnus-auto-select-next 'slightly-quietly)
5946                  push)
5947             (and (eq gnus-auto-select-next 'almost-quietly)
5948                  (gnus-summary-last-article-p)))
5949         ;; Select quietly.
5950         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5951             (gnus-summary-exit)
5952           (gnus-message 7 "No more%s articles (%s)..."
5953                         (if unread " unread" "")
5954                         (if group (concat "selecting " group)
5955                           "exiting"))
5956           (gnus-summary-next-group nil group backward)))
5957        (t
5958         (when (gnus-key-press-event-p last-input-event)
5959           (gnus-summary-walk-group-buffer
5960            gnus-newsgroup-name cmd unread backward point))))))))
5961
5962 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5963   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5964                       (?\C-p (gnus-group-prev-unread-group 1))))
5965         (cursor-in-echo-area t)
5966         keve key group ended)
5967     (save-excursion
5968       (set-buffer gnus-group-buffer)
5969       (goto-char start)
5970       (setq group
5971             (if (eq gnus-keep-same-level 'best)
5972                 (gnus-summary-best-group gnus-newsgroup-name)
5973               (gnus-summary-search-group backward gnus-keep-same-level))))
5974     (while (not ended)
5975       (gnus-message
5976        5 "No more%s articles%s" (if unread " unread" "")
5977        (if (and group
5978                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5979            (format " (Type %s for %s [%s])"
5980                    (single-key-description cmd) group
5981                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5982          (format " (Type %s to exit %s)"
5983                  (single-key-description cmd)
5984                  gnus-newsgroup-name)))
5985       ;; Confirm auto selection.
5986       (setq key (car (setq keve (gnus-read-event-char))))
5987       (setq ended t)
5988       (cond
5989        ((assq key keystrokes)
5990         (let ((obuf (current-buffer)))
5991           (switch-to-buffer gnus-group-buffer)
5992           (when group
5993             (gnus-group-jump-to-group group))
5994           (eval (cadr (assq key keystrokes)))
5995           (setq group (gnus-group-group-name))
5996           (switch-to-buffer obuf))
5997         (setq ended nil))
5998        ((equal key cmd)
5999         (if (or (not group)
6000                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6001             (gnus-summary-exit)
6002           (gnus-summary-next-group nil group backward)))
6003        (t
6004         (push (cdr keve) unread-command-events))))))
6005
6006 (defun gnus-summary-next-unread-article ()
6007   "Select unread article after current one."
6008   (interactive)
6009   (gnus-summary-next-article
6010    (or (not (eq gnus-summary-goto-unread 'never))
6011        (gnus-summary-last-article-p (gnus-summary-article-number)))
6012    (and gnus-auto-select-same
6013         (gnus-summary-article-subject))))
6014
6015 (defun gnus-summary-prev-article (&optional unread subject)
6016   "Select the article after the current one.
6017 If UNREAD is non-nil, only unread articles are selected."
6018   (interactive "P")
6019   (gnus-summary-next-article unread subject t))
6020
6021 (defun gnus-summary-prev-unread-article ()
6022   "Select unread article before current one."
6023   (interactive)
6024   (gnus-summary-prev-article
6025    (or (not (eq gnus-summary-goto-unread 'never))
6026        (gnus-summary-first-article-p (gnus-summary-article-number)))
6027    (and gnus-auto-select-same
6028         (gnus-summary-article-subject))))
6029
6030 (defun gnus-summary-next-page (&optional lines circular)
6031   "Show next page of the selected article.
6032 If at the end of the current article, select the next article.
6033 LINES says how many lines should be scrolled up.
6034
6035 If CIRCULAR is non-nil, go to the start of the article instead of
6036 selecting the next article when reaching the end of the current
6037 article."
6038   (interactive "P")
6039   (setq gnus-summary-buffer (current-buffer))
6040   (gnus-set-global-variables)
6041   (let ((article (gnus-summary-article-number))
6042         (article-window (get-buffer-window gnus-article-buffer t))
6043         endp)
6044     ;; If the buffer is empty, we have no article.
6045     (unless article
6046       (error "No article to select"))
6047     (gnus-configure-windows 'article)
6048     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6049         (if (and (eq gnus-summary-goto-unread 'never)
6050                  (not (gnus-summary-last-article-p article)))
6051             (gnus-summary-next-article)
6052           (gnus-summary-next-unread-article))
6053       (if (or (null gnus-current-article)
6054               (null gnus-article-current)
6055               (/= article (cdr gnus-article-current))
6056               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6057           ;; Selected subject is different from current article's.
6058           (gnus-summary-display-article article)
6059         (when article-window
6060           (gnus-eval-in-buffer-window gnus-article-buffer
6061             (setq endp (gnus-article-next-page lines)))
6062           (when endp
6063             (cond (circular
6064                    (gnus-summary-beginning-of-article))
6065                   (lines
6066                    (gnus-message 3 "End of message"))
6067                   ((null lines)
6068                    (if (and (eq gnus-summary-goto-unread 'never)
6069                             (not (gnus-summary-last-article-p article)))
6070                        (gnus-summary-next-article)
6071                      (gnus-summary-next-unread-article))))))))
6072     (gnus-summary-recenter)
6073     (gnus-summary-position-point)))
6074
6075 (defun gnus-summary-prev-page (&optional lines move)
6076   "Show previous page of selected article.
6077 Argument LINES specifies lines to be scrolled down.
6078 If MOVE, move to the previous unread article if point is at
6079 the beginning of the buffer."
6080   (interactive "P")
6081   (let ((article (gnus-summary-article-number))
6082         (article-window (get-buffer-window gnus-article-buffer t))
6083         endp)
6084     (gnus-configure-windows 'article)
6085     (if (or (null gnus-current-article)
6086             (null gnus-article-current)
6087             (/= article (cdr gnus-article-current))
6088             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6089         ;; Selected subject is different from current article's.
6090         (gnus-summary-display-article article)
6091       (gnus-summary-recenter)
6092       (when article-window
6093         (gnus-eval-in-buffer-window gnus-article-buffer
6094           (setq endp (gnus-article-prev-page lines)))
6095         (when (and move endp)
6096           (cond (lines
6097                  (gnus-message 3 "Beginning of message"))
6098                 ((null lines)
6099                  (if (and (eq gnus-summary-goto-unread 'never)
6100                           (not (gnus-summary-first-article-p article)))
6101                      (gnus-summary-prev-article)
6102                    (gnus-summary-prev-unread-article))))))))
6103   (gnus-summary-position-point))
6104
6105 (defun gnus-summary-prev-page-or-article (&optional lines)
6106   "Show previous page of selected article.
6107 Argument LINES specifies lines to be scrolled down.
6108 If at the beginning of the article, go to the next article."
6109   (interactive "P")
6110   (gnus-summary-prev-page lines t))
6111
6112 (defun gnus-summary-scroll-up (lines)
6113   "Scroll up (or down) one line current article.
6114 Argument LINES specifies lines to be scrolled up (or down if negative)."
6115   (interactive "p")
6116   (gnus-configure-windows 'article)
6117   (gnus-summary-show-thread)
6118   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6119     (gnus-eval-in-buffer-window gnus-article-buffer
6120       (cond ((> lines 0)
6121              (when (gnus-article-next-page lines)
6122                (gnus-message 3 "End of message")))
6123             ((< lines 0)
6124              (gnus-article-prev-page (- lines))))))
6125   (gnus-summary-recenter)
6126   (gnus-summary-position-point))
6127
6128 (defun gnus-summary-scroll-down (lines)
6129   "Scroll down (or up) one line current article.
6130 Argument LINES specifies lines to be scrolled down (or up if negative)."
6131   (interactive "p")
6132   (gnus-summary-scroll-up (- lines)))
6133
6134 (defun gnus-summary-next-same-subject ()
6135   "Select next article which has the same subject as current one."
6136   (interactive)
6137   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6138
6139 (defun gnus-summary-prev-same-subject ()
6140   "Select previous article which has the same subject as current one."
6141   (interactive)
6142   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6143
6144 (defun gnus-summary-next-unread-same-subject ()
6145   "Select next unread article which has the same subject as current one."
6146   (interactive)
6147   (gnus-summary-next-article t (gnus-summary-article-subject)))
6148
6149 (defun gnus-summary-prev-unread-same-subject ()
6150   "Select previous unread article which has the same subject as current one."
6151   (interactive)
6152   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6153
6154 (defun gnus-summary-first-unread-article ()
6155   "Select the first unread article.
6156 Return nil if there are no unread articles."
6157   (interactive)
6158   (prog1
6159       (when (gnus-summary-first-subject t)
6160         (gnus-summary-show-thread)
6161         (gnus-summary-first-subject t)
6162         (gnus-summary-display-article (gnus-summary-article-number)))
6163     (gnus-summary-position-point)))
6164
6165 (defun gnus-summary-first-unread-subject ()
6166   "Place the point on the subject line of the first unread article.
6167 Return nil if there are no unread articles."
6168   (interactive)
6169   (prog1
6170       (when (gnus-summary-first-subject t)
6171         (gnus-summary-show-thread)
6172         (gnus-summary-first-subject t))
6173     (gnus-summary-position-point)))
6174
6175 (defun gnus-summary-first-article ()
6176   "Select the first article.
6177 Return nil if there are no articles."
6178   (interactive)
6179   (prog1
6180       (when (gnus-summary-first-subject)
6181         (gnus-summary-show-thread)
6182         (gnus-summary-first-subject)
6183         (gnus-summary-display-article (gnus-summary-article-number)))
6184     (gnus-summary-position-point)))
6185
6186 (defun gnus-summary-best-unread-article ()
6187   "Select the unread article with the highest score."
6188   (interactive)
6189   (let ((best -1000000)
6190         (data gnus-newsgroup-data)
6191         article score)
6192     (while data
6193       (and (gnus-data-unread-p (car data))
6194            (> (setq score
6195                     (gnus-summary-article-score (gnus-data-number (car data))))
6196               best)
6197            (setq best score
6198                  article (gnus-data-number (car data))))
6199       (setq data (cdr data)))
6200     (prog1
6201         (if article
6202             (gnus-summary-goto-article article)
6203           (error "No unread articles"))
6204       (gnus-summary-position-point))))
6205
6206 (defun gnus-summary-last-subject ()
6207   "Go to the last displayed subject line in the group."
6208   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6209     (when article
6210       (gnus-summary-goto-subject article))))
6211
6212 (defun gnus-summary-goto-article (article &optional all-headers force)
6213   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6214 If ALL-HEADERS is non-nil, no header lines are hidden.
6215 If FORCE, go to the article even if it isn't displayed.  If FORCE
6216 is a number, it is the line the article is to be displayed on."
6217   (interactive
6218    (list
6219     (completing-read
6220      "Article number or Message-ID: "
6221      (mapcar (lambda (number) (list (int-to-string number)))
6222              gnus-newsgroup-limit))
6223     current-prefix-arg
6224     t))
6225   (prog1
6226       (if (and (stringp article)
6227                (string-match "@" article))
6228           (gnus-summary-refer-article article)
6229         (when (stringp article)
6230           (setq article (string-to-number article)))
6231         (if (gnus-summary-goto-subject article force)
6232             (gnus-summary-display-article article all-headers)
6233           (gnus-message 4 "Couldn't go to article %s" article) nil))
6234     (gnus-summary-position-point)))
6235
6236 (defun gnus-summary-goto-last-article ()
6237   "Go to the previously read article."
6238   (interactive)
6239   (prog1
6240       (when gnus-last-article
6241         (gnus-summary-goto-article gnus-last-article nil t))
6242     (gnus-summary-position-point)))
6243
6244 (defun gnus-summary-pop-article (number)
6245   "Pop one article off the history and go to the previous.
6246 NUMBER articles will be popped off."
6247   (interactive "p")
6248   (let (to)
6249     (setq gnus-newsgroup-history
6250           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6251     (if to
6252         (gnus-summary-goto-article (car to) nil t)
6253       (error "Article history empty")))
6254   (gnus-summary-position-point))
6255
6256 ;; Summary commands and functions for limiting the summary buffer.
6257
6258 (defun gnus-summary-limit-to-articles (n)
6259   "Limit the summary buffer to the next N articles.
6260 If not given a prefix, use the process marked articles instead."
6261   (interactive "P")
6262   (prog1
6263       (let ((articles (gnus-summary-work-articles n)))
6264         (setq gnus-newsgroup-processable nil)
6265         (gnus-summary-limit articles))
6266     (gnus-summary-position-point)))
6267
6268 (defun gnus-summary-pop-limit (&optional total)
6269   "Restore the previous limit.
6270 If given a prefix, remove all limits."
6271   (interactive "P")
6272   (when total
6273     (setq gnus-newsgroup-limits
6274           (list (mapcar (lambda (h) (mail-header-number h))
6275                         gnus-newsgroup-headers))))
6276   (unless gnus-newsgroup-limits
6277     (error "No limit to pop"))
6278   (prog1
6279       (gnus-summary-limit nil 'pop)
6280     (gnus-summary-position-point)))
6281
6282 (defun gnus-summary-limit-to-subject (subject &optional header)
6283   "Limit the summary buffer to articles that have subjects that match a regexp."
6284   (interactive "sLimit to subject (regexp): ")
6285   (unless header
6286     (setq header "subject"))
6287   (when (not (equal "" subject))
6288     (prog1
6289         (let ((articles (gnus-summary-find-matching
6290                          (or header "subject") subject 'all)))
6291           (unless articles
6292             (error "Found no matches for \"%s\"" subject))
6293           (gnus-summary-limit articles))
6294       (gnus-summary-position-point))))
6295
6296 (defun gnus-summary-limit-to-author (from)
6297   "Limit the summary buffer to articles that have authors that match a regexp."
6298   (interactive "sLimit to author (regexp): ")
6299   (gnus-summary-limit-to-subject from "from"))
6300
6301 (defun gnus-summary-limit-to-age (age &optional younger-p)
6302   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6303 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6304 articles that are younger than AGE days."
6305   (interactive
6306    (let ((younger current-prefix-arg)
6307          (days-got nil)
6308          days)
6309      (while (not days-got)
6310        (setq days (if younger
6311                       (read-string "Limit to articles within (in days): ")
6312                     (read-string "Limit to articles old than (in days): ")))
6313        (when (> (length days) 0)
6314          (setq days (read days)))
6315        (if (numberp days)
6316            (setq days-got t)
6317          (message "Please enter a number.")
6318          (sleep-for 1)))
6319      (list days younger)))
6320   (prog1
6321       (let ((data gnus-newsgroup-data)
6322             (cutoff (days-to-time age))
6323             articles d date is-younger)
6324         (while (setq d (pop data))
6325           (when (and (vectorp (gnus-data-header d))
6326                      (setq date (mail-header-date (gnus-data-header d))))
6327             (setq is-younger (time-less-p
6328                               (time-since (condition-case ()
6329                                               (date-to-time date)
6330                                             (error '(0 0))))
6331                               cutoff))
6332             (when (if younger-p
6333                       is-younger
6334                     (not is-younger))
6335               (push (gnus-data-number d) articles))))
6336         (gnus-summary-limit (nreverse articles)))
6337     (gnus-summary-position-point)))
6338
6339 (defun gnus-summary-limit-to-extra (header regexp)
6340   "Limit the summary buffer to articles that match an 'extra' header."
6341   (interactive
6342    (let ((header
6343           (intern
6344            (gnus-completing-read
6345             (symbol-name (car gnus-extra-headers))
6346             "Limit extra header:"
6347             (mapcar (lambda (x)
6348                       (cons (symbol-name x) x))
6349                     gnus-extra-headers)
6350             nil
6351             t))))
6352      (list header
6353            (read-string (format "Limit to header %s (regexp): " header)))))
6354   (when (not (equal "" regexp))
6355     (prog1
6356         (let ((articles (gnus-summary-find-matching
6357                          (cons 'extra header) regexp 'all)))
6358           (unless articles
6359             (error "Found no matches for \"%s\"" regexp))
6360           (gnus-summary-limit articles))
6361       (gnus-summary-position-point))))
6362
6363 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6364 (make-obsolete
6365  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6366
6367 (defun gnus-summary-limit-to-unread (&optional all)
6368   "Limit the summary buffer to articles that are not marked as read.
6369 If ALL is non-nil, limit strictly to unread articles."
6370   (interactive "P")
6371   (if all
6372       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6373     (gnus-summary-limit-to-marks
6374      ;; Concat all the marks that say that an article is read and have
6375      ;; those removed.
6376      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6377            gnus-killed-mark gnus-kill-file-mark
6378            gnus-low-score-mark gnus-expirable-mark
6379            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6380            gnus-duplicate-mark gnus-souped-mark)
6381      'reverse)))
6382
6383 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6384 (make-obsolete 'gnus-summary-delete-marked-with
6385                'gnus-summary-limit-exlude-marks)
6386
6387 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6388   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6389 If REVERSE, limit the summary buffer to articles that are marked
6390 with MARKS.  MARKS can either be a string of marks or a list of marks.
6391 Returns how many articles were removed."
6392   (interactive "sMarks: ")
6393   (gnus-summary-limit-to-marks marks t))
6394
6395 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6396   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6397 If REVERSE (the prefix), limit the summary buffer to articles that are
6398 not marked with MARKS.  MARKS can either be a string of marks or a
6399 list of marks.
6400 Returns how many articles were removed."
6401   (interactive "sMarks: \nP")
6402   (prog1
6403       (let ((data gnus-newsgroup-data)
6404             (marks (if (listp marks) marks
6405                      (append marks nil))) ; Transform to list.
6406             articles)
6407         (while data
6408           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6409                   (memq (gnus-data-mark (car data)) marks))
6410             (push (gnus-data-number (car data)) articles))
6411           (setq data (cdr data)))
6412         (gnus-summary-limit articles))
6413     (gnus-summary-position-point)))
6414
6415 (defun gnus-summary-limit-to-score (&optional score)
6416   "Limit to articles with score at or above SCORE."
6417   (interactive "P")
6418   (setq score (if score
6419                   (prefix-numeric-value score)
6420                 (or gnus-summary-default-score 0)))
6421   (let ((data gnus-newsgroup-data)
6422         articles)
6423     (while data
6424       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6425                 score)
6426         (push (gnus-data-number (car data)) articles))
6427       (setq data (cdr data)))
6428     (prog1
6429         (gnus-summary-limit articles)
6430       (gnus-summary-position-point))))
6431
6432 (defun gnus-summary-limit-include-thread (id)
6433   "Display all the hidden articles that in the current thread."
6434   (interactive (list (mail-header-id (gnus-summary-article-header))))
6435   (let ((articles (gnus-articles-in-thread
6436                    (gnus-id-to-thread (gnus-root-id id)))))
6437     (prog1
6438         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6439       (gnus-summary-position-point))))
6440
6441 (defun gnus-summary-limit-include-dormant ()
6442   "Display all the hidden articles that are marked as dormant.
6443 Note that this command only works on a subset of the articles currently
6444 fetched for this group."
6445   (interactive)
6446   (unless gnus-newsgroup-dormant
6447     (error "There are no dormant articles in this group"))
6448   (prog1
6449       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6450     (gnus-summary-position-point)))
6451
6452 (defun gnus-summary-limit-exclude-dormant ()
6453   "Hide all dormant articles."
6454   (interactive)
6455   (prog1
6456       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6457     (gnus-summary-position-point)))
6458
6459 (defun gnus-summary-limit-exclude-childless-dormant ()
6460   "Hide all dormant articles that have no children."
6461   (interactive)
6462   (let ((data (gnus-data-list t))
6463         articles d children)
6464     ;; Find all articles that are either not dormant or have
6465     ;; children.
6466     (while (setq d (pop data))
6467       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6468                 (and (setq children
6469                            (gnus-article-children (gnus-data-number d)))
6470                      (let (found)
6471                        (while children
6472                          (when (memq (car children) articles)
6473                            (setq children nil
6474                                  found t))
6475                          (pop children))
6476                        found)))
6477         (push (gnus-data-number d) articles)))
6478     ;; Do the limiting.
6479     (prog1
6480         (gnus-summary-limit articles)
6481       (gnus-summary-position-point))))
6482
6483 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6484   "Mark all unread excluded articles as read.
6485 If ALL, mark even excluded ticked and dormants as read."
6486   (interactive "P")
6487   (let ((articles (gnus-sorted-complement
6488                    (sort
6489                     (mapcar (lambda (h) (mail-header-number h))
6490                             gnus-newsgroup-headers)
6491                     '<)
6492                    (sort gnus-newsgroup-limit '<)))
6493         article)
6494     (setq gnus-newsgroup-unreads
6495           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6496     (if all
6497         (setq gnus-newsgroup-dormant nil
6498               gnus-newsgroup-marked nil
6499               gnus-newsgroup-reads
6500               (nconc
6501                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6502                gnus-newsgroup-reads))
6503       (while (setq article (pop articles))
6504         (unless (or (memq article gnus-newsgroup-dormant)
6505                     (memq article gnus-newsgroup-marked))
6506           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6507
6508 (defun gnus-summary-limit (articles &optional pop)
6509   (if pop
6510       ;; We pop the previous limit off the stack and use that.
6511       (setq articles (car gnus-newsgroup-limits)
6512             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6513     ;; We use the new limit, so we push the old limit on the stack.
6514     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6515   ;; Set the limit.
6516   (setq gnus-newsgroup-limit articles)
6517   (let ((total (length gnus-newsgroup-data))
6518         (data (gnus-data-find-list (gnus-summary-article-number)))
6519         (gnus-summary-mark-below nil)   ; Inhibit this.
6520         found)
6521     ;; This will do all the work of generating the new summary buffer
6522     ;; according to the new limit.
6523     (gnus-summary-prepare)
6524     ;; Hide any threads, possibly.
6525     (and gnus-show-threads
6526          gnus-thread-hide-subtree
6527          (gnus-summary-hide-all-threads))
6528     ;; Try to return to the article you were at, or one in the
6529     ;; neighborhood.
6530     (when data
6531       ;; We try to find some article after the current one.
6532       (while data
6533         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6534           (setq data nil
6535                 found t))
6536         (setq data (cdr data))))
6537     (unless found
6538       ;; If there is no data, that means that we were after the last
6539       ;; article.  The same goes when we can't find any articles
6540       ;; after the current one.
6541       (goto-char (point-max))
6542       (gnus-summary-find-prev))
6543     (gnus-set-mode-line 'summary)
6544     ;; We return how many articles were removed from the summary
6545     ;; buffer as a result of the new limit.
6546     (- total (length gnus-newsgroup-data))))
6547
6548 (defsubst gnus-invisible-cut-children (threads)
6549   (let ((num 0))
6550     (while threads
6551       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6552         (incf num))
6553       (pop threads))
6554     (< num 2)))
6555
6556 (defsubst gnus-cut-thread (thread)
6557   "Go forwards in the thread until we find an article that we want to display."
6558   (when (or (eq gnus-fetch-old-headers 'some)
6559             (eq gnus-fetch-old-headers 'invisible)
6560             (numberp gnus-fetch-old-headers)
6561             (eq gnus-build-sparse-threads 'some)
6562             (eq gnus-build-sparse-threads 'more))
6563     ;; Deal with old-fetched headers and sparse threads.
6564     (while (and
6565             thread
6566             (or
6567              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6568              (gnus-summary-article-ancient-p
6569               (mail-header-number (car thread))))
6570             (if (or (<= (length (cdr thread)) 1)
6571                     (eq gnus-fetch-old-headers 'invisible))
6572                 (setq gnus-newsgroup-limit
6573                       (delq (mail-header-number (car thread))
6574                             gnus-newsgroup-limit)
6575                       thread (cadr thread))
6576               (when (gnus-invisible-cut-children (cdr thread))
6577                 (let ((th (cdr thread)))
6578                   (while th
6579                     (if (memq (mail-header-number (caar th))
6580                               gnus-newsgroup-limit)
6581                         (setq thread (car th)
6582                               th nil)
6583                       (setq th (cdr th))))))))))
6584   thread)
6585
6586 (defun gnus-cut-threads (threads)
6587   "Cut off all uninteresting articles from the beginning of threads."
6588   (when (or (eq gnus-fetch-old-headers 'some)
6589             (eq gnus-fetch-old-headers 'invisible)
6590             (numberp gnus-fetch-old-headers)
6591             (eq gnus-build-sparse-threads 'some)
6592             (eq gnus-build-sparse-threads 'more))
6593     (let ((th threads))
6594       (while th
6595         (setcar th (gnus-cut-thread (car th)))
6596         (setq th (cdr th)))))
6597   ;; Remove nixed out threads.
6598   (delq nil threads))
6599
6600 (defun gnus-summary-initial-limit (&optional show-if-empty)
6601   "Figure out what the initial limit is supposed to be on group entry.
6602 This entails weeding out unwanted dormants, low-scored articles,
6603 fetch-old-headers verbiage, and so on."
6604   ;; Most groups have nothing to remove.
6605   (if (or gnus-inhibit-limiting
6606           (and (null gnus-newsgroup-dormant)
6607                (not (eq gnus-fetch-old-headers 'some))
6608                (not (numberp gnus-fetch-old-headers))
6609                (not (eq gnus-fetch-old-headers 'invisible))
6610                (null gnus-summary-expunge-below)
6611                (not (eq gnus-build-sparse-threads 'some))
6612                (not (eq gnus-build-sparse-threads 'more))
6613                (null gnus-thread-expunge-below)
6614                (not gnus-use-nocem)))
6615       ()                                ; Do nothing.
6616     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6617     (setq gnus-newsgroup-limit nil)
6618     (mapatoms
6619      (lambda (node)
6620        (unless (car (symbol-value node))
6621          ;; These threads have no parents -- they are roots.
6622          (let ((nodes (cdr (symbol-value node)))
6623                thread)
6624            (while nodes
6625              (if (and gnus-thread-expunge-below
6626                       (< (gnus-thread-total-score (car nodes))
6627                          gnus-thread-expunge-below))
6628                  (gnus-expunge-thread (pop nodes))
6629                (setq thread (pop nodes))
6630                (gnus-summary-limit-children thread))))))
6631      gnus-newsgroup-dependencies)
6632     ;; If this limitation resulted in an empty group, we might
6633     ;; pop the previous limit and use it instead.
6634     (when (and (not gnus-newsgroup-limit)
6635                show-if-empty)
6636       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6637     gnus-newsgroup-limit))
6638
6639 (defun gnus-summary-limit-children (thread)
6640   "Return 1 if this subthread is visible and 0 if it is not."
6641   ;; First we get the number of visible children to this thread.  This
6642   ;; is done by recursing down the thread using this function, so this
6643   ;; will really go down to a leaf article first, before slowly
6644   ;; working its way up towards the root.
6645   (when thread
6646     (let ((children
6647            (if (cdr thread)
6648                (apply '+ (mapcar 'gnus-summary-limit-children
6649                                  (cdr thread)))
6650              0))
6651           (number (mail-header-number (car thread)))
6652           score)
6653       (if (and
6654            (not (memq number gnus-newsgroup-marked))
6655            (or
6656             ;; If this article is dormant and has absolutely no visible
6657             ;; children, then this article isn't visible.
6658             (and (memq number gnus-newsgroup-dormant)
6659                  (zerop children))
6660             ;; If this is "fetch-old-headered" and there is no
6661             ;; visible children, then we don't want this article.
6662             (and (or (eq gnus-fetch-old-headers 'some)
6663                      (numberp gnus-fetch-old-headers))
6664                  (gnus-summary-article-ancient-p number)
6665                  (zerop children))
6666             ;; If this is "fetch-old-headered" and `invisible', then
6667             ;; we don't want this article.
6668             (and (eq gnus-fetch-old-headers 'invisible)
6669                  (gnus-summary-article-ancient-p number))
6670             ;; If this is a sparsely inserted article with no children,
6671             ;; we don't want it.
6672             (and (eq gnus-build-sparse-threads 'some)
6673                  (gnus-summary-article-sparse-p number)
6674                  (zerop children))
6675             ;; If we use expunging, and this article is really
6676             ;; low-scored, then we don't want this article.
6677             (when (and gnus-summary-expunge-below
6678                        (< (setq score
6679                                 (or (cdr (assq number gnus-newsgroup-scored))
6680                                     gnus-summary-default-score))
6681                           gnus-summary-expunge-below))
6682               ;; We increase the expunge-tally here, but that has
6683               ;; nothing to do with the limits, really.
6684               (incf gnus-newsgroup-expunged-tally)
6685               ;; We also mark as read here, if that's wanted.
6686               (when (and gnus-summary-mark-below
6687                          (< score gnus-summary-mark-below))
6688                 (setq gnus-newsgroup-unreads
6689                       (delq number gnus-newsgroup-unreads))
6690                 (if gnus-newsgroup-auto-expire
6691                     (push number gnus-newsgroup-expirable)
6692                   (push (cons number gnus-low-score-mark)
6693                         gnus-newsgroup-reads)))
6694               t)
6695             ;; Check NoCeM things.
6696             (if (and gnus-use-nocem
6697                      (gnus-nocem-unwanted-article-p
6698                       (mail-header-id (car thread))))
6699                 (progn
6700                   (setq gnus-newsgroup-unreads
6701                         (delq number gnus-newsgroup-unreads))
6702                   t))))
6703           ;; Nope, invisible article.
6704           0
6705         ;; Ok, this article is to be visible, so we add it to the limit
6706         ;; and return 1.
6707         (push number gnus-newsgroup-limit)
6708         1))))
6709
6710 (defun gnus-expunge-thread (thread)
6711   "Mark all articles in THREAD as read."
6712   (let* ((number (mail-header-number (car thread))))
6713     (incf gnus-newsgroup-expunged-tally)
6714     ;; We also mark as read here, if that's wanted.
6715     (setq gnus-newsgroup-unreads
6716           (delq number gnus-newsgroup-unreads))
6717     (if gnus-newsgroup-auto-expire
6718         (push number gnus-newsgroup-expirable)
6719       (push (cons number gnus-low-score-mark)
6720             gnus-newsgroup-reads)))
6721   ;; Go recursively through all subthreads.
6722   (mapcar 'gnus-expunge-thread (cdr thread)))
6723
6724 ;; Summary article oriented commands
6725
6726 (defun gnus-summary-refer-parent-article (n)
6727   "Refer parent article N times.
6728 If N is negative, go to ancestor -N instead.
6729 The difference between N and the number of articles fetched is returned."
6730   (interactive "p")
6731   (let ((skip 1)
6732         error header ref)
6733     (when (not (natnump n))
6734       (setq skip (abs n)
6735             n 1))
6736     (while (and (> n 0)
6737                 (not error))
6738       (setq header (gnus-summary-article-header))
6739       (if (and (eq (mail-header-number header)
6740                    (cdr gnus-article-current))
6741                (equal gnus-newsgroup-name
6742                       (car gnus-article-current)))
6743           ;; If we try to find the parent of the currently
6744           ;; displayed article, then we take a look at the actual
6745           ;; References header, since this is slightly more
6746           ;; reliable than the References field we got from the
6747           ;; server.
6748           (save-excursion
6749             (set-buffer gnus-original-article-buffer)
6750             (nnheader-narrow-to-headers)
6751             (unless (setq ref (message-fetch-field "references"))
6752               (setq ref (message-fetch-field "in-reply-to")))
6753             (widen))
6754         (setq ref
6755               ;; It's not the current article, so we take a bet on
6756               ;; the value we got from the server.
6757               (mail-header-references header)))
6758       (if (and ref
6759                (not (equal ref "")))
6760           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6761             (gnus-message 1 "Couldn't find parent"))
6762         (gnus-message 1 "No references in article %d"
6763                       (gnus-summary-article-number))
6764         (setq error t))
6765       (decf n))
6766     (gnus-summary-position-point)
6767     n))
6768
6769 (defun gnus-summary-refer-references ()
6770   "Fetch all articles mentioned in the References header.
6771 Return the number of articles fetched."
6772   (interactive)
6773   (let ((ref (mail-header-references (gnus-summary-article-header)))
6774         (current (gnus-summary-article-number))
6775         (n 0))
6776     (if (or (not ref)
6777             (equal ref ""))
6778         (error "No References in the current article")
6779       ;; For each Message-ID in the References header...
6780       (while (string-match "<[^>]*>" ref)
6781         (incf n)
6782         ;; ... fetch that article.
6783         (gnus-summary-refer-article
6784          (prog1 (match-string 0 ref)
6785            (setq ref (substring ref (match-end 0))))))
6786       (gnus-summary-goto-subject current)
6787       (gnus-summary-position-point)
6788       n)))
6789
6790 (defun gnus-summary-refer-thread (&optional limit)
6791   "Fetch all articles in the current thread.
6792 If LIMIT (the numerical prefix), fetch that many old headers instead
6793 of what's specified by the `gnus-refer-thread-limit' variable."
6794   (interactive "P")
6795   (let ((id (mail-header-id (gnus-summary-article-header)))
6796         (limit (if limit (prefix-numeric-value limit)
6797                  gnus-refer-thread-limit)))
6798     ;; We want to fetch LIMIT *old* headers, but we also have to
6799     ;; re-fetch all the headers in the current buffer, because many of
6800     ;; them may be undisplayed.  So we adjust LIMIT.
6801     (when (numberp limit)
6802       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6803     (unless (eq gnus-fetch-old-headers 'invisible)
6804       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6805       ;; Retrieve the headers and read them in.
6806       (if (eq (gnus-retrieve-headers
6807                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6808               'nov)
6809           (gnus-build-all-threads)
6810         (error "Can't fetch thread from backends that don't support NOV"))
6811       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6812     (gnus-summary-limit-include-thread id)))
6813
6814 (defun gnus-summary-refer-article (message-id)
6815   "Fetch an article specified by MESSAGE-ID."
6816   (interactive "sMessage-ID: ")
6817   (when (and (stringp message-id)
6818              (not (zerop (length message-id))))
6819     ;; Construct the correct Message-ID if necessary.
6820     ;; Suggested by tale@pawl.rpi.edu.
6821     (unless (string-match "^<" message-id)
6822       (setq message-id (concat "<" message-id)))
6823     (unless (string-match ">$" message-id)
6824       (setq message-id (concat message-id ">")))
6825     (let* ((header (gnus-id-to-header message-id))
6826            (sparse (and header
6827                         (gnus-summary-article-sparse-p
6828                          (mail-header-number header))
6829                         (memq (mail-header-number header)
6830                               gnus-newsgroup-limit)))
6831            number)
6832       (cond
6833        ;; If the article is present in the buffer we just go to it.
6834        ((and header
6835              (or (not (gnus-summary-article-sparse-p
6836                        (mail-header-number header)))
6837                  sparse))
6838         (prog1
6839             (gnus-summary-goto-article
6840              (mail-header-number header) nil t)
6841           (when sparse
6842             (gnus-summary-update-article (mail-header-number header)))))
6843        (t
6844         ;; We fetch the article.
6845         (catch 'found
6846           (dolist (gnus-override-method (gnus-refer-article-methods))
6847             (gnus-check-server gnus-override-method)
6848             ;; Fetch the header, and display the article.
6849             (when (setq number (gnus-summary-insert-subject message-id))
6850               (gnus-summary-select-article nil nil nil number)
6851               (throw 'found t)))
6852           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6853
6854 (defun gnus-refer-article-methods ()
6855   "Return a list of referrable methods."
6856   (cond
6857    ;; No method, so we default to current and native.
6858    ((null gnus-refer-article-method)
6859     (list gnus-current-select-method gnus-select-method))
6860    ;; Current.
6861    ((eq 'current gnus-refer-article-method)
6862     (list gnus-current-select-method))
6863    ;; List of select methods.
6864    ((not (stringp (cadr gnus-refer-article-method)))
6865     (let (out)
6866       (dolist (method gnus-refer-article-method)
6867         (push (if (eq 'current method)
6868                   gnus-current-select-method
6869                 method)
6870               out))
6871       (nreverse out)))
6872    ;; One single select method.
6873    (t
6874     (list gnus-refer-article-method))))
6875
6876 (defun gnus-summary-edit-parameters ()
6877   "Edit the group parameters of the current group."
6878   (interactive)
6879   (gnus-group-edit-group gnus-newsgroup-name 'params))
6880
6881 (defun gnus-summary-customize-parameters ()
6882   "Customize the group parameters of the current group."
6883   (interactive)
6884   (gnus-group-customize gnus-newsgroup-name))
6885
6886 (defun gnus-summary-enter-digest-group (&optional force)
6887   "Enter an nndoc group based on the current article.
6888 If FORCE, force a digest interpretation.  If not, try
6889 to guess what the document format is."
6890   (interactive "P")
6891   (let ((conf gnus-current-window-configuration))
6892     (save-excursion
6893       (gnus-summary-select-article))
6894     (setq gnus-current-window-configuration conf)
6895     (let* ((name (format "%s-%d"
6896                          (gnus-group-prefixed-name
6897                           gnus-newsgroup-name (list 'nndoc ""))
6898                          (save-excursion
6899                            (set-buffer gnus-summary-buffer)
6900                            gnus-current-article)))
6901            (ogroup gnus-newsgroup-name)
6902            (params (append (gnus-info-params (gnus-get-info ogroup))
6903                            (list (cons 'to-group ogroup))
6904                            (list (cons 'save-article-group ogroup))))
6905            (case-fold-search t)
6906            (buf (current-buffer))
6907            dig to-address)
6908       (save-excursion
6909         (set-buffer gnus-original-article-buffer)
6910         ;; Have the digest group inherit the main mail address of
6911         ;; the parent article.
6912         (when (setq to-address (or (message-fetch-field "reply-to")
6913                                    (message-fetch-field "from")))
6914           (setq params (append (list (cons 'to-address to-address)))))
6915         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6916         (insert-buffer-substring gnus-original-article-buffer)
6917         ;; Remove lines that may lead nndoc to misinterpret the
6918         ;; document type.
6919         (narrow-to-region
6920          (goto-char (point-min))
6921          (or (search-forward "\n\n" nil t) (point)))
6922         (goto-char (point-min))
6923         (delete-matching-lines "^Path:\\|^From ")
6924         (widen))
6925       (unwind-protect
6926           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6927                     (gnus-newsgroup-ephemeral-ignored-charsets
6928                      gnus-newsgroup-ignored-charsets))
6929                 (gnus-group-read-ephemeral-group
6930                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6931                               (nndoc-article-type
6932                                ,(if force 'mbox 'guess))) t))
6933               ;; Make all postings to this group go to the parent group.
6934               (nconc (gnus-info-params (gnus-get-info name))
6935                      params)
6936             ;; Couldn't select this doc group.
6937             (switch-to-buffer buf)
6938             (gnus-set-global-variables)
6939             (gnus-configure-windows 'summary)
6940             (gnus-message 3 "Article couldn't be entered?"))
6941         (kill-buffer dig)))))
6942
6943 (defun gnus-summary-read-document (n)
6944   "Open a new group based on the current article(s).
6945 This will allow you to read digests and other similar
6946 documents as newsgroups.
6947 Obeys the standard process/prefix convention."
6948   (interactive "P")
6949   (let* ((articles (gnus-summary-work-articles n))
6950          (ogroup gnus-newsgroup-name)
6951          (params (append (gnus-info-params (gnus-get-info ogroup))
6952                          (list (cons 'to-group ogroup))))
6953          article group egroup groups vgroup)
6954     (while (setq article (pop articles))
6955       (setq group (format "%s-%d" gnus-newsgroup-name article))
6956       (gnus-summary-remove-process-mark article)
6957       (when (gnus-summary-display-article article)
6958         (save-excursion
6959           (with-temp-buffer
6960             (insert-buffer-substring gnus-original-article-buffer)
6961             ;; Remove some headers that may lead nndoc to make
6962             ;; the wrong guess.
6963             (message-narrow-to-head)
6964             (goto-char (point-min))
6965             (delete-matching-lines "^\\(Path\\):\\|^From ")
6966             (widen)
6967             (if (setq egroup
6968                       (gnus-group-read-ephemeral-group
6969                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6970                                      (nndoc-article-type guess))
6971                        t nil t))
6972                 (progn
6973                   ;; Make all postings to this group go to the parent group.
6974                   (nconc (gnus-info-params (gnus-get-info egroup))
6975                          params)
6976                   (push egroup groups))
6977               ;; Couldn't select this doc group.
6978               (gnus-error 3 "Article couldn't be entered"))))))
6979     ;; Now we have selected all the documents.
6980     (cond
6981      ((not groups)
6982       (error "None of the articles could be interpreted as documents"))
6983      ((gnus-group-read-ephemeral-group
6984        (setq vgroup (format
6985                      "nnvirtual:%s-%s" gnus-newsgroup-name
6986                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6987        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6988        t
6989        (cons (current-buffer) 'summary)))
6990      (t
6991       (error "Couldn't select virtual nndoc group")))))
6992
6993 (defun gnus-summary-isearch-article (&optional regexp-p)
6994   "Do incremental search forward on the current article.
6995 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6996   (interactive "P")
6997   (gnus-summary-select-article)
6998   (gnus-configure-windows 'article)
6999   (gnus-eval-in-buffer-window gnus-article-buffer
7000     (save-restriction
7001       (widen)
7002       (isearch-forward regexp-p))))
7003
7004 (defun gnus-summary-search-article-forward (regexp &optional backward)
7005   "Search for an article containing REGEXP forward.
7006 If BACKWARD, search backward instead."
7007   (interactive
7008    (list (read-string
7009           (format "Search article %s (regexp%s): "
7010                   (if current-prefix-arg "backward" "forward")
7011                   (if gnus-last-search-regexp
7012                       (concat ", default " gnus-last-search-regexp)
7013                     "")))
7014          current-prefix-arg))
7015   (if (string-equal regexp "")
7016       (setq regexp (or gnus-last-search-regexp ""))
7017     (setq gnus-last-search-regexp regexp))
7018   (if (gnus-summary-search-article regexp backward)
7019       (gnus-summary-show-thread)
7020     (error "Search failed: \"%s\"" regexp)))
7021
7022 (defun gnus-summary-search-article-backward (regexp)
7023   "Search for an article containing REGEXP backward."
7024   (interactive
7025    (list (read-string
7026           (format "Search article backward (regexp%s): "
7027                   (if gnus-last-search-regexp
7028                       (concat ", default " gnus-last-search-regexp)
7029                     "")))))
7030   (gnus-summary-search-article-forward regexp 'backward))
7031
7032 (defun gnus-summary-search-article (regexp &optional backward)
7033   "Search for an article containing REGEXP.
7034 Optional argument BACKWARD means do search for backward.
7035 `gnus-select-article-hook' is not called during the search."
7036   ;; We have to require this here to make sure that the following
7037   ;; dynamic binding isn't shadowed by autoloading.
7038   (require 'gnus-async)
7039   (require 'gnus-art)
7040   (let ((gnus-select-article-hook nil)  ;Disable hook.
7041         (gnus-article-prepare-hook nil)
7042         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7043         (gnus-use-article-prefetch nil)
7044         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7045         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7046         (sum (current-buffer))
7047         (gnus-display-mime-function nil)
7048         (found nil)
7049         point)
7050     (gnus-save-hidden-threads
7051       (gnus-summary-select-article)
7052       (set-buffer gnus-article-buffer)
7053       (goto-char (window-point (get-buffer-window (current-buffer))))
7054       (when backward
7055         (forward-line -1))
7056       (while (not found)
7057         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7058         (if (if backward
7059                 (re-search-backward regexp nil t)
7060               (re-search-forward regexp nil t))
7061             ;; We found the regexp.
7062             (progn
7063               (setq found 'found)
7064               (beginning-of-line)
7065               (set-window-start
7066                (get-buffer-window (current-buffer))
7067                (point))
7068               (forward-line 1)
7069               (set-window-point
7070                (get-buffer-window (current-buffer))
7071                (point))
7072               (set-buffer sum)
7073               (setq point (point)))
7074           ;; We didn't find it, so we go to the next article.
7075           (set-buffer sum)
7076           (setq found 'not)
7077           (while (eq found 'not)
7078             (if (not (if backward (gnus-summary-find-prev)
7079                        (gnus-summary-find-next)))
7080                 ;; No more articles.
7081                 (setq found t)
7082               ;; Select the next article and adjust point.
7083               (unless (gnus-summary-article-sparse-p
7084                        (gnus-summary-article-number))
7085                 (setq found nil)
7086                 (gnus-summary-select-article)
7087                 (set-buffer gnus-article-buffer)
7088                 (widen)
7089                 (goto-char (if backward (point-max) (point-min))))))))
7090       (gnus-message 7 ""))
7091     ;; Return whether we found the regexp.
7092     (when (eq found 'found)
7093       (goto-char point)
7094       (gnus-summary-show-thread)
7095       (gnus-summary-goto-subject gnus-current-article)
7096       (gnus-summary-position-point)
7097       t)))
7098
7099 (defun gnus-summary-find-matching (header regexp &optional backward unread
7100                                           not-case-fold)
7101   "Return a list of all articles that match REGEXP on HEADER.
7102 The search stars on the current article and goes forwards unless
7103 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7104 If UNREAD is non-nil, only unread articles will
7105 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7106 in the comparisons."
7107   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7108                 (gnus-data-find-list
7109                  (gnus-summary-article-number) (gnus-data-list backward))))
7110         (case-fold-search (not not-case-fold))
7111         articles d func)
7112     (if (consp header)
7113         (if (eq (car header) 'extra)
7114             (setq func
7115                   `(lambda (h)
7116                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7117                          "")))
7118           (error "%s is an invalid header" header))
7119       (unless (fboundp (intern (concat "mail-header-" header)))
7120         (error "%s is not a valid header" header))
7121       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7122     (while data
7123       (setq d (car data))
7124       (and (or (not unread)             ; We want all articles...
7125                (gnus-data-unread-p d))  ; Or just unreads.
7126            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7127            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7128            (push (gnus-data-number d) articles)) ; Success!
7129       (setq data (cdr data)))
7130     (nreverse articles)))
7131
7132 (defun gnus-summary-execute-command (header regexp command &optional backward)
7133   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7134 If HEADER is an empty string (or nil), the match is done on the entire
7135 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7136   (interactive
7137    (list (let ((completion-ignore-case t))
7138            (completing-read
7139             "Header name: "
7140             (mapcar (lambda (string) (list string))
7141                     '("Number" "Subject" "From" "Lines" "Date"
7142                       "Message-ID" "Xref" "References" "Body"))
7143             nil 'require-match))
7144          (read-string "Regexp: ")
7145          (read-key-sequence "Command: ")
7146          current-prefix-arg))
7147   (when (equal header "Body")
7148     (setq header ""))
7149   ;; Hidden thread subtrees must be searched as well.
7150   (gnus-summary-show-all-threads)
7151   ;; We don't want to change current point nor window configuration.
7152   (save-excursion
7153     (save-window-excursion
7154       (gnus-message 6 "Executing %s..." (key-description command))
7155       ;; We'd like to execute COMMAND interactively so as to give arguments.
7156       (gnus-execute header regexp
7157                     `(call-interactively ',(key-binding command))
7158                     backward)
7159       (gnus-message 6 "Executing %s...done" (key-description command)))))
7160
7161 (defun gnus-summary-beginning-of-article ()
7162   "Scroll the article back to the beginning."
7163   (interactive)
7164   (gnus-summary-select-article)
7165   (gnus-configure-windows 'article)
7166   (gnus-eval-in-buffer-window gnus-article-buffer
7167     (widen)
7168     (goto-char (point-min))
7169     (when gnus-page-broken
7170       (gnus-narrow-to-page))))
7171
7172 (defun gnus-summary-end-of-article ()
7173   "Scroll to the end of the article."
7174   (interactive)
7175   (gnus-summary-select-article)
7176   (gnus-configure-windows 'article)
7177   (gnus-eval-in-buffer-window gnus-article-buffer
7178     (widen)
7179     (goto-char (point-max))
7180     (recenter -3)
7181     (when gnus-page-broken
7182       (gnus-narrow-to-page))))
7183
7184 (defun gnus-summary-print-article (&optional filename n)
7185   "Generate and print a PostScript image of the N next (mail) articles.
7186
7187 If N is negative, print the N previous articles.  If N is nil and articles
7188 have been marked with the process mark, print these instead.
7189
7190 If the optional first argument FILENAME is nil, send the image to the
7191 printer.  If FILENAME is a string, save the PostScript image in a file with
7192 that name.  If FILENAME is a number, prompt the user for the name of the file
7193 to save in."
7194   (interactive (list (ps-print-preprint current-prefix-arg)
7195                      current-prefix-arg))
7196   (dolist (article (gnus-summary-work-articles n))
7197     (gnus-summary-select-article nil nil 'pseudo article)
7198     (gnus-eval-in-buffer-window gnus-article-buffer
7199       (let ((buffer (generate-new-buffer " *print*")))
7200         (unwind-protect
7201             (progn
7202               (copy-to-buffer buffer (point-min) (point-max))
7203               (set-buffer buffer)
7204               (gnus-article-delete-invisible-text)
7205               (let ((ps-left-header
7206                      (list
7207                       (concat "("
7208                               (mail-header-subject gnus-current-headers) ")")
7209                       (concat "("
7210                               (mail-header-from gnus-current-headers) ")")))
7211                     (ps-right-header
7212                      (list
7213                       "/pagenumberstring load"
7214                       (concat "("
7215                               (mail-header-date gnus-current-headers) ")"))))
7216                 (gnus-run-hooks 'gnus-ps-print-hook)
7217                 (save-excursion
7218                   (ps-print-buffer-with-faces filename))))
7219           (kill-buffer buffer))))))
7220
7221 (defun gnus-summary-show-article (&optional arg)
7222   "Force re-fetching of the current article.
7223 If ARG (the prefix) is a number, show the article with the charset
7224 defined in `gnus-summary-show-article-charset-alist', or the charset
7225 inputed.
7226 If ARG (the prefix) is non-nil and not a number, show the raw article
7227 without any article massaging functions being run."
7228   (interactive "P")
7229   (cond
7230    ((numberp arg)
7231     (let ((gnus-newsgroup-charset
7232            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7233                (read-coding-system "Charset: ")))
7234           (gnus-newsgroup-ignored-charsets 'gnus-all))
7235       (gnus-summary-select-article nil 'force)))
7236    ((not arg)
7237     ;; Select the article the normal way.
7238     (gnus-summary-select-article nil 'force))
7239    (t
7240     ;; We have to require this here to make sure that the following
7241     ;; dynamic binding isn't shadowed by autoloading.
7242     (require 'gnus-async)
7243     (require 'gnus-art)
7244     ;; Bind the article treatment functions to nil.
7245     (let ((gnus-have-all-headers t)
7246           gnus-article-prepare-hook
7247           gnus-article-decode-hook
7248           gnus-display-mime-function
7249           gnus-break-pages)
7250       ;; Destroy any MIME parts.
7251       (when (gnus-buffer-live-p gnus-article-buffer)
7252         (save-excursion
7253           (set-buffer gnus-article-buffer)
7254           (mm-destroy-parts gnus-article-mime-handles)
7255           (setq gnus-article-mime-handles nil)))
7256       (gnus-summary-select-article nil 'force))))
7257   (gnus-summary-goto-subject gnus-current-article)
7258   (gnus-summary-position-point))
7259
7260 (defun gnus-summary-verbose-headers (&optional arg)
7261   "Toggle permanent full header display.
7262 If ARG is a positive number, turn header display on.
7263 If ARG is a negative number, turn header display off."
7264   (interactive "P")
7265   (setq gnus-show-all-headers
7266         (cond ((or (not (numberp arg))
7267                    (zerop arg))
7268                (not gnus-show-all-headers))
7269               ((natnump arg)
7270                t)))
7271   (gnus-summary-show-article))
7272
7273 (defun gnus-summary-toggle-header (&optional arg)
7274   "Show the headers if they are hidden, or hide them if they are shown.
7275 If ARG is a positive number, show the entire header.
7276 If ARG is a negative number, hide the unwanted header lines."
7277   (interactive "P")
7278   (save-excursion
7279     (set-buffer gnus-article-buffer)
7280     (save-restriction
7281       (let* ((buffer-read-only nil)
7282              (inhibit-point-motion-hooks t)
7283              hidden e)
7284         (setq hidden
7285               (if (numberp arg)
7286                   (>= arg 0)
7287                 (save-restriction
7288                   (article-narrow-to-head)
7289                   (gnus-article-hidden-text-p 'headers))))
7290         (goto-char (point-min))
7291         (when (search-forward "\n\n" nil t)
7292           (delete-region (point-min) (1- (point))))
7293         (goto-char (point-min))
7294         (save-excursion
7295           (set-buffer gnus-original-article-buffer)
7296           (goto-char (point-min))
7297           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7298         (insert-buffer-substring gnus-original-article-buffer 1 e)
7299         (save-restriction
7300           (narrow-to-region (point-min) (point))
7301           (article-decode-encoded-words)
7302           (if  hidden
7303               (let ((gnus-treat-hide-headers nil)
7304                     (gnus-treat-hide-boring-headers nil))
7305                 (setq gnus-article-wash-types
7306                       (delq 'headers gnus-article-wash-types))
7307                 (gnus-treat-article 'head))
7308             (gnus-treat-article 'head)))
7309         (gnus-set-mode-line 'article)))))
7310
7311 (defun gnus-summary-show-all-headers ()
7312   "Make all header lines visible."
7313   (interactive)
7314   (gnus-article-show-all-headers))
7315
7316 (defun gnus-summary-caesar-message (&optional arg)
7317   "Caesar rotate the current article by 13.
7318 The numerical prefix specifies how many places to rotate each letter
7319 forward."
7320   (interactive "P")
7321   (gnus-summary-select-article)
7322   (let ((mail-header-separator ""))
7323     (gnus-eval-in-buffer-window gnus-article-buffer
7324       (save-restriction
7325         (widen)
7326         (let ((start (window-start))
7327               buffer-read-only)
7328           (message-caesar-buffer-body arg)
7329           (set-window-start (get-buffer-window (current-buffer)) start))))))
7330
7331 (defun gnus-summary-stop-page-breaking ()
7332   "Stop page breaking in the current article."
7333   (interactive)
7334   (gnus-summary-select-article)
7335   (gnus-eval-in-buffer-window gnus-article-buffer
7336     (widen)
7337     (when (gnus-visual-p 'page-marker)
7338       (let ((buffer-read-only nil))
7339         (gnus-remove-text-with-property 'gnus-prev)
7340         (gnus-remove-text-with-property 'gnus-next))
7341       (setq gnus-page-broken nil))))
7342
7343 (defun gnus-summary-move-article (&optional n to-newsgroup
7344                                             select-method action)
7345   "Move the current article to a different newsgroup.
7346 If N is a positive number, move the N next articles.
7347 If N is a negative number, move the N previous articles.
7348 If N is nil and any articles have been marked with the process mark,
7349 move those articles instead.
7350 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7351 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7352 re-spool using this method.
7353
7354 For this function to work, both the current newsgroup and the
7355 newsgroup that you want to move to have to support the `request-move'
7356 and `request-accept' functions.
7357
7358 ACTION can be either `move' (the default), `crosspost' or `copy'."
7359   (interactive "P")
7360   (unless action
7361     (setq action 'move))
7362   ;; Disable marking as read.
7363   (let (gnus-mark-article-hook)
7364     (save-window-excursion
7365       (gnus-summary-select-article)))
7366   ;; Check whether the source group supports the required functions.
7367   (cond ((and (eq action 'move)
7368               (not (gnus-check-backend-function
7369                     'request-move-article gnus-newsgroup-name)))
7370          (error "The current group does not support article moving"))
7371         ((and (eq action 'crosspost)
7372               (not (gnus-check-backend-function
7373                     'request-replace-article gnus-newsgroup-name)))
7374          (error "The current group does not support article editing")))
7375   (let ((articles (gnus-summary-work-articles n))
7376         (prefix (if (gnus-check-backend-function
7377                     'request-move-article gnus-newsgroup-name)
7378                     (gnus-group-real-prefix gnus-newsgroup-name)
7379                   ""))
7380         (names '((move "Move" "Moving")
7381                  (copy "Copy" "Copying")
7382                  (crosspost "Crosspost" "Crossposting")))
7383         (copy-buf (save-excursion
7384                     (nnheader-set-temp-buffer " *copy article*")))
7385         art-group to-method new-xref article to-groups)
7386     (unless (assq action names)
7387       (error "Unknown action %s" action))
7388     ;; Read the newsgroup name.
7389     (when (and (not to-newsgroup)
7390                (not select-method))
7391       (setq to-newsgroup
7392             (gnus-read-move-group-name
7393              (cadr (assq action names))
7394              (symbol-value (intern (format "gnus-current-%s-group" action)))
7395              articles prefix))
7396       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7397     (setq to-method (or select-method
7398                         (gnus-server-to-method
7399                          (gnus-group-method to-newsgroup))))
7400     ;; Check the method we are to move this article to...
7401     (unless (gnus-check-backend-function
7402              'request-accept-article (car to-method))
7403       (error "%s does not support article copying" (car to-method)))
7404     (unless (gnus-check-server to-method)
7405       (error "Can't open server %s" (car to-method)))
7406     (gnus-message 6 "%s to %s: %s..."
7407                   (caddr (assq action names))
7408                   (or (car select-method) to-newsgroup) articles)
7409     (while articles
7410       (setq article (pop articles))
7411       (setq
7412        art-group
7413        (cond
7414         ;; Move the article.
7415         ((eq action 'move)
7416          ;; Remove this article from future suppression.
7417          (gnus-dup-unsuppress-article article)
7418          (gnus-request-move-article
7419           article                       ; Article to move
7420           gnus-newsgroup-name           ; From newsgroup
7421           (nth 1 (gnus-find-method-for-group
7422                   gnus-newsgroup-name)) ; Server
7423           (list 'gnus-request-accept-article
7424                 to-newsgroup (list 'quote select-method)
7425                 (not articles) t)       ; Accept form
7426           (not articles)))              ; Only save nov last time
7427         ;; Copy the article.
7428         ((eq action 'copy)
7429          (save-excursion
7430            (set-buffer copy-buf)
7431            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7432              (gnus-request-accept-article
7433               to-newsgroup select-method (not articles) t))))
7434         ;; Crosspost the article.
7435         ((eq action 'crosspost)
7436          (let ((xref (message-tokenize-header
7437                       (mail-header-xref (gnus-summary-article-header article))
7438                       " ")))
7439            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7440                                   ":" article))
7441            (unless xref
7442              (setq xref (list (system-name))))
7443            (setq new-xref
7444                  (concat
7445                   (mapconcat 'identity
7446                              (delete "Xref:" (delete new-xref xref))
7447                              " ")
7448                   " " new-xref))
7449            (save-excursion
7450              (set-buffer copy-buf)
7451              ;; First put the article in the destination group.
7452              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7453              (when (consp (setq art-group
7454                                 (gnus-request-accept-article
7455                                  to-newsgroup select-method (not articles))))
7456                (setq new-xref (concat new-xref " " (car art-group)
7457                                       ":" (cdr art-group)))
7458                ;; Now we have the new Xrefs header, so we insert
7459                ;; it and replace the new article.
7460                (nnheader-replace-header "Xref" new-xref)
7461                (gnus-request-replace-article
7462                 (cdr art-group) to-newsgroup (current-buffer))
7463                art-group))))))
7464       (cond
7465        ((not art-group)
7466         (gnus-message 1 "Couldn't %s article %s: %s"
7467                       (cadr (assq action names)) article
7468                       (nnheader-get-report (car to-method))))
7469        ((eq art-group 'junk)
7470         (when (eq action 'move)
7471           (gnus-summary-mark-article article gnus-canceled-mark)
7472           (gnus-message 4 "Deleted article %s" article)))
7473        (t
7474         (let* ((pto-group (gnus-group-prefixed-name
7475                            (car art-group) to-method))
7476                (entry
7477                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7478                (info (nth 2 entry))
7479                (to-group (gnus-info-group info))
7480                to-marks)
7481           ;; Update the group that has been moved to.
7482           (when (and info
7483                      (memq action '(move copy)))
7484             (unless (member to-group to-groups)
7485               (push to-group to-groups))
7486
7487             (unless (memq article gnus-newsgroup-unreads)
7488               (push 'read to-marks)
7489               (gnus-info-set-read
7490                info (gnus-add-to-range (gnus-info-read info)
7491                                        (list (cdr art-group)))))
7492
7493             ;; See whether the article is to be put in the cache.
7494             (let ((marks gnus-article-mark-lists)
7495                   (to-article (cdr art-group)))
7496
7497               ;; Enter the article into the cache in the new group,
7498               ;; if that is required.
7499               (when gnus-use-cache
7500                 (gnus-cache-possibly-enter-article
7501                  to-group to-article
7502                  (memq article gnus-newsgroup-marked)
7503                  (memq article gnus-newsgroup-dormant)
7504                  (memq article gnus-newsgroup-unreads)))
7505
7506               (when gnus-preserve-marks
7507                 ;; Copy any marks over to the new group.
7508                 (when (and (equal to-group gnus-newsgroup-name)
7509                            (not (memq article gnus-newsgroup-unreads)))
7510                   ;; Mark this article as read in this group.
7511                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7512                   (setcdr (gnus-active to-group) to-article)
7513                   (setcdr gnus-newsgroup-active to-article))
7514
7515                 (while marks
7516                   (when (memq article (symbol-value
7517                                        (intern (format "gnus-newsgroup-%s"
7518                                                        (caar marks)))))
7519                     (push (cdar marks) to-marks)
7520                     ;; If the other group is the same as this group,
7521                     ;; then we have to add the mark to the list.
7522                     (when (equal to-group gnus-newsgroup-name)
7523                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7524                            (cons to-article
7525                                  (symbol-value
7526                                   (intern (format "gnus-newsgroup-%s"
7527                                                   (caar marks)))))))
7528                     ;; Copy the marks to other group.
7529                     (gnus-add-marked-articles
7530                      to-group (cdar marks) (list to-article) info))
7531                   (setq marks (cdr marks)))
7532
7533                 (gnus-request-set-mark to-group (list (list (list to-article)
7534                                                             'set
7535                                                             to-marks))))
7536
7537               (gnus-dribble-enter
7538                (concat "(gnus-group-set-info '"
7539                        (gnus-prin1-to-string (gnus-get-info to-group))
7540                        ")"))))
7541
7542           ;; Update the Xref header in this article to point to
7543           ;; the new crossposted article we have just created.
7544           (when (eq action 'crosspost)
7545             (save-excursion
7546               (set-buffer copy-buf)
7547               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7548               (nnheader-replace-header "Xref" new-xref)
7549               (gnus-request-replace-article
7550                article gnus-newsgroup-name (current-buffer)))))
7551
7552         ;;;!!!Why is this necessary?
7553         (set-buffer gnus-summary-buffer)
7554
7555         (gnus-summary-goto-subject article)
7556         (when (eq action 'move)
7557           (gnus-summary-mark-article article gnus-canceled-mark))))
7558       (gnus-summary-remove-process-mark article))
7559     ;; Re-activate all groups that have been moved to.
7560     (while to-groups
7561       (save-excursion
7562         (set-buffer gnus-group-buffer)
7563         (when (gnus-group-goto-group (car to-groups) t)
7564           (gnus-group-get-new-news-this-group 1 t))
7565         (pop to-groups)))
7566
7567     (gnus-kill-buffer copy-buf)
7568     (gnus-summary-position-point)
7569     (gnus-set-mode-line 'summary)))
7570
7571 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7572   "Move the current article to a different newsgroup.
7573 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7574 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7575 re-spool using this method."
7576   (interactive "P")
7577   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7578
7579 (defun gnus-summary-crosspost-article (&optional n)
7580   "Crosspost the current article to some other group."
7581   (interactive "P")
7582   (gnus-summary-move-article n nil nil 'crosspost))
7583
7584 (defcustom gnus-summary-respool-default-method nil
7585   "Default method for respooling an article.
7586 If nil, use to the current newsgroup method."
7587   :type '(choice (gnus-select-method :value (nnml ""))
7588                  (const nil))
7589   :group 'gnus-summary-mail)
7590
7591 (defun gnus-summary-respool-article (&optional n method)
7592   "Respool the current article.
7593 The article will be squeezed through the mail spooling process again,
7594 which means that it will be put in some mail newsgroup or other
7595 depending on `nnmail-split-methods'.
7596 If N is a positive number, respool the N next articles.
7597 If N is a negative number, respool the N previous articles.
7598 If N is nil and any articles have been marked with the process mark,
7599 respool those articles instead.
7600
7601 Respooling can be done both from mail groups and \"real\" newsgroups.
7602 In the former case, the articles in question will be moved from the
7603 current group into whatever groups they are destined to.  In the
7604 latter case, they will be copied into the relevant groups."
7605   (interactive
7606    (list current-prefix-arg
7607          (let* ((methods (gnus-methods-using 'respool))
7608                 (methname
7609                  (symbol-name (or gnus-summary-respool-default-method
7610                                   (car (gnus-find-method-for-group
7611                                         gnus-newsgroup-name)))))
7612                 (method
7613                  (gnus-completing-read
7614                   methname "What backend do you want to use when respooling?"
7615                   methods nil t nil 'gnus-mail-method-history))
7616                 ms)
7617            (cond
7618             ((zerop (length (setq ms (gnus-servers-using-backend
7619                                       (intern method)))))
7620              (list (intern method) ""))
7621             ((= 1 (length ms))
7622              (car ms))
7623             (t
7624              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7625                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7626                            ms-alist))))))))
7627   (unless method
7628     (error "No method given for respooling"))
7629   (if (assoc (symbol-name
7630               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7631              (gnus-methods-using 'respool))
7632       (gnus-summary-move-article n nil method)
7633     (gnus-summary-copy-article n nil method)))
7634
7635 (defun gnus-summary-import-article (file)
7636   "Import an arbitrary file into a mail newsgroup."
7637   (interactive "fImport file: ")
7638   (let ((group gnus-newsgroup-name)
7639         (now (current-time))
7640         atts lines)
7641     (unless (gnus-check-backend-function 'request-accept-article group)
7642       (error "%s does not support article importing" group))
7643     (or (file-readable-p file)
7644         (not (file-regular-p file))
7645         (error "Can't read %s" file))
7646     (save-excursion
7647       (set-buffer (gnus-get-buffer-create " *import file*"))
7648       (erase-buffer)
7649       (nnheader-insert-file-contents file)
7650       (goto-char (point-min))
7651       (unless (nnheader-article-p)
7652         ;; This doesn't look like an article, so we fudge some headers.
7653         (setq atts (file-attributes file)
7654               lines (count-lines (point-min) (point-max)))
7655         (insert "From: " (read-string "From: ") "\n"
7656                 "Subject: " (read-string "Subject: ") "\n"
7657                 "Date: " (message-make-date (nth 5 atts))
7658                 "\n"
7659                 "Message-ID: " (message-make-message-id) "\n"
7660                 "Lines: " (int-to-string lines) "\n"
7661                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7662       (gnus-request-accept-article group nil t)
7663       (kill-buffer (current-buffer)))))
7664
7665 (defun gnus-summary-article-posted-p ()
7666   "Say whether the current (mail) article is available from news as well.
7667 This will be the case if the article has both been mailed and posted."
7668   (interactive)
7669   (let ((id (mail-header-references (gnus-summary-article-header)))
7670         (gnus-override-method (car (gnus-refer-article-methods))))
7671     (if (gnus-request-head id "")
7672         (gnus-message 2 "The current message was found on %s"
7673                       gnus-override-method)
7674       (gnus-message 2 "The current message couldn't be found on %s"
7675                     gnus-override-method)
7676       nil)))
7677
7678 (defun gnus-summary-expire-articles (&optional now)
7679   "Expire all articles that are marked as expirable in the current group."
7680   (interactive)
7681   (when (gnus-check-backend-function
7682          'request-expire-articles gnus-newsgroup-name)
7683     ;; This backend supports expiry.
7684     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7685            (expirable (if total
7686                           (progn
7687                             ;; We need to update the info for
7688                             ;; this group for `gnus-list-of-read-articles'
7689                             ;; to give us the right answer.
7690                             (gnus-run-hooks 'gnus-exit-group-hook)
7691                             (gnus-summary-update-info)
7692                             (gnus-list-of-read-articles gnus-newsgroup-name))
7693                         (setq gnus-newsgroup-expirable
7694                               (sort gnus-newsgroup-expirable '<))))
7695            (expiry-wait (if now 'immediate
7696                           (gnus-group-find-parameter
7697                            gnus-newsgroup-name 'expiry-wait)))
7698            (nnmail-expiry-target
7699             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
7700                 nnmail-expiry-target))
7701            es)
7702       (when expirable
7703         ;; There are expirable articles in this group, so we run them
7704         ;; through the expiry process.
7705         (gnus-message 6 "Expiring articles...")
7706         (unless (gnus-check-group gnus-newsgroup-name)
7707           (error "Can't open server for %s" gnus-newsgroup-name))
7708         ;; The list of articles that weren't expired is returned.
7709         (save-excursion
7710           (if expiry-wait
7711               (let ((nnmail-expiry-wait-function nil)
7712                     (nnmail-expiry-wait expiry-wait))
7713                 (setq es (gnus-request-expire-articles
7714                           expirable gnus-newsgroup-name)))
7715             (setq es (gnus-request-expire-articles
7716                       expirable gnus-newsgroup-name))))
7717         (unless total
7718           (setq gnus-newsgroup-expirable es))
7719         ;; We go through the old list of expirable, and mark all
7720         ;; really expired articles as nonexistent.
7721         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7722           (let ((gnus-use-cache nil))
7723             (while expirable
7724               (unless (memq (car expirable) es)
7725                 (when (gnus-data-find (car expirable))
7726                   (gnus-summary-mark-article
7727                    (car expirable) gnus-canceled-mark)))
7728               (setq expirable (cdr expirable)))))
7729         (gnus-message 6 "Expiring articles...done")))))
7730
7731 (defun gnus-summary-expire-articles-now ()
7732   "Expunge all expirable articles in the current group.
7733 This means that *all* articles that are marked as expirable will be
7734 deleted forever, right now."
7735   (interactive)
7736   (or gnus-expert-user
7737       (gnus-yes-or-no-p
7738        "Are you really, really, really sure you want to delete all these messages? ")
7739       (error "Phew!"))
7740   (gnus-summary-expire-articles t))
7741
7742 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7743 (defun gnus-summary-delete-article (&optional n)
7744   "Delete the N next (mail) articles.
7745 This command actually deletes articles.  This is not a marking
7746 command.  The article will disappear forever from your life, never to
7747 return.
7748 If N is negative, delete backwards.
7749 If N is nil and articles have been marked with the process mark,
7750 delete these instead."
7751   (interactive "P")
7752   (unless (gnus-check-backend-function 'request-expire-articles
7753                                        gnus-newsgroup-name)
7754     (error "The current newsgroup does not support article deletion"))
7755   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
7756     (error "Couldn't open server"))
7757   ;; Compute the list of articles to delete.
7758   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7759         not-deleted)
7760     (if (and gnus-novice-user
7761              (not (gnus-yes-or-no-p
7762                    (format "Do you really want to delete %s forever? "
7763                            (if (> (length articles) 1)
7764                                (format "these %s articles" (length articles))
7765                              "this article")))))
7766         ()
7767       ;; Delete the articles.
7768       (setq not-deleted (gnus-request-expire-articles
7769                          articles gnus-newsgroup-name 'force))
7770       (while articles
7771         (gnus-summary-remove-process-mark (car articles))
7772         ;; The backend might not have been able to delete the article
7773         ;; after all.
7774         (unless (memq (car articles) not-deleted)
7775           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7776         (setq articles (cdr articles)))
7777       (when not-deleted
7778         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7779     (gnus-summary-position-point)
7780     (gnus-set-mode-line 'summary)
7781     not-deleted))
7782
7783 (defun gnus-summary-edit-article (&optional arg)
7784   "Edit the current article.
7785 This will have permanent effect only in mail groups.
7786 If ARG is nil, edit the decoded articles.
7787 If ARG is 1, edit the raw articles. 
7788 If ARG is 2, edit the raw articles even in read-only groups.
7789 Otherwise, allow editing of articles even in read-only
7790 groups."
7791   (interactive "P")
7792   (let (force raw)
7793     (cond 
7794      ((null arg))
7795      ((eq arg 1) (setq raw t))
7796      ((eq arg 2) (setq raw t
7797                        force t))
7798      (t (setq force t)))
7799     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
7800         (error "Can't edit the raw article in group nndraft:drafts."))
7801     (save-excursion
7802       (set-buffer gnus-summary-buffer)
7803       (let ((mail-parse-charset gnus-newsgroup-charset)
7804             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7805         (gnus-set-global-variables)
7806         (when (and (not force)
7807                    (gnus-group-read-only-p))
7808           (error "The current newsgroup does not support article editing"))
7809         (gnus-summary-show-article t)
7810         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
7811           (with-current-buffer gnus-article-buffer
7812             (mm-enable-multibyte)))
7813         (if (equal gnus-newsgroup-name "nndraft:drafts")
7814             (setq raw t))
7815         (gnus-article-edit-article
7816          (if raw 'ignore 
7817            #'(lambda () 
7818                (let ((mbl mml-buffer-list))
7819                  (setq mml-buffer-list nil)
7820                  (mime-to-mml)
7821                  (make-local-hook 'kill-buffer-hook)
7822                  (let ((mml-buffer-list mml-buffer-list))
7823                    (setq mml-buffer-list mbl)
7824                    (make-local-variable 'mml-buffer-list))
7825                  (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
7826          `(lambda (no-highlight)
7827             (let ((mail-parse-charset ',gnus-newsgroup-charset)
7828                   (mail-parse-ignored-charsets 
7829                    ',gnus-newsgroup-ignored-charsets))
7830               ,(if (not raw) '(progn 
7831                                 (mml-to-mime)
7832                                 (mml-destroy-buffers)
7833                                 (remove-hook 'kill-buffer-hook 
7834                                              'mml-destroy-buffers t)
7835                                 (kill-local-variable 'mml-buffer-list)))
7836               (gnus-summary-edit-article-done
7837                ,(or (mail-header-references gnus-current-headers) "")
7838                ,(gnus-group-read-only-p) 
7839                ,gnus-summary-buffer no-highlight))))))))
7840
7841 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7842
7843 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7844                                                  no-highlight)
7845   "Make edits to the current article permanent."
7846   (interactive)
7847   ;; Replace the article.
7848   (let ((buf (current-buffer)))
7849     (with-temp-buffer
7850       (insert-buffer-substring buf)
7851       (if (and (not read-only)
7852                (not (gnus-request-replace-article
7853                      (cdr gnus-article-current) (car gnus-article-current)
7854                      (current-buffer) t)))
7855           (error "Couldn't replace article")
7856         ;; Update the summary buffer.
7857         (if (and references
7858                  (equal (message-tokenize-header references " ")
7859                         (message-tokenize-header
7860                          (or (message-fetch-field "references") "") " ")))
7861             ;; We only have to update this line.
7862             (save-excursion
7863               (save-restriction
7864                 (message-narrow-to-head)
7865                 (let ((head (buffer-string))
7866                       header)
7867                   (with-temp-buffer
7868                     (insert (format "211 %d Article retrieved.\n"
7869                                     (cdr gnus-article-current)))
7870                     (insert head)
7871                     (insert ".\n")
7872                     (let ((nntp-server-buffer (current-buffer)))
7873                       (setq header (car (gnus-get-newsgroup-headers
7874                                          (save-excursion
7875                                            (set-buffer gnus-summary-buffer)
7876                                            gnus-newsgroup-dependencies)
7877                                          t))))
7878                     (save-excursion
7879                       (set-buffer gnus-summary-buffer)
7880                       (gnus-data-set-header
7881                        (gnus-data-find (cdr gnus-article-current))
7882                        header)
7883                       (gnus-summary-update-article-line
7884                        (cdr gnus-article-current) header))))))
7885           ;; Update threads.
7886           (set-buffer (or buffer gnus-summary-buffer))
7887           (gnus-summary-update-article (cdr gnus-article-current)))
7888         ;; Prettify the article buffer again.
7889         (unless no-highlight
7890           (save-excursion
7891             (set-buffer gnus-article-buffer)
7892             ;;;!!! Fix this -- article should be rehighlighted.
7893             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7894             (set-buffer gnus-original-article-buffer)
7895             (gnus-request-article
7896              (cdr gnus-article-current)
7897              (car gnus-article-current) (current-buffer))))
7898         ;; Prettify the summary buffer line.
7899         (when (gnus-visual-p 'summary-highlight 'highlight)
7900           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7901
7902 (defun gnus-summary-edit-wash (key)
7903   "Perform editing command KEY in the article buffer."
7904   (interactive
7905    (list
7906     (progn
7907       (message "%s" (concat (this-command-keys) "- "))
7908       (read-char))))
7909   (message "")
7910   (gnus-summary-edit-article)
7911   (execute-kbd-macro (concat (this-command-keys) key))
7912   (gnus-article-edit-done))
7913
7914 ;;; Respooling
7915
7916 (defun gnus-summary-respool-query (&optional silent trace)
7917   "Query where the respool algorithm would put this article."
7918   (interactive)
7919   (let (gnus-mark-article-hook)
7920     (gnus-summary-select-article)
7921     (save-excursion
7922       (set-buffer gnus-original-article-buffer)
7923       (save-restriction
7924         (message-narrow-to-head)
7925         (let ((groups (nnmail-article-group 'identity trace)))
7926           (unless silent
7927             (if groups
7928                 (message "This message would go to %s"
7929                          (mapconcat 'car groups ", "))
7930               (message "This message would go to no groups"))
7931             groups))))))
7932
7933 (defun gnus-summary-respool-trace ()
7934   "Trace where the respool algorithm would put this article.
7935 Display a buffer showing all fancy splitting patterns which matched."
7936   (interactive)
7937   (gnus-summary-respool-query nil t))
7938
7939 ;; Summary marking commands.
7940
7941 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7942   "Mark articles which has the same subject as read, and then select the next.
7943 If UNMARK is positive, remove any kind of mark.
7944 If UNMARK is negative, tick articles."
7945   (interactive "P")
7946   (when unmark
7947     (setq unmark (prefix-numeric-value unmark)))
7948   (let ((count
7949          (gnus-summary-mark-same-subject
7950           (gnus-summary-article-subject) unmark)))
7951     ;; Select next unread article.  If auto-select-same mode, should
7952     ;; select the first unread article.
7953     (gnus-summary-next-article t (and gnus-auto-select-same
7954                                       (gnus-summary-article-subject)))
7955     (gnus-message 7 "%d article%s marked as %s"
7956                   count (if (= count 1) " is" "s are")
7957                   (if unmark "unread" "read"))))
7958
7959 (defun gnus-summary-kill-same-subject (&optional unmark)
7960   "Mark articles which has the same subject as read.
7961 If UNMARK is positive, remove any kind of mark.
7962 If UNMARK is negative, tick articles."
7963   (interactive "P")
7964   (when unmark
7965     (setq unmark (prefix-numeric-value unmark)))
7966   (let ((count
7967          (gnus-summary-mark-same-subject
7968           (gnus-summary-article-subject) unmark)))
7969     ;; If marked as read, go to next unread subject.
7970     (when (null unmark)
7971       ;; Go to next unread subject.
7972       (gnus-summary-next-subject 1 t))
7973     (gnus-message 7 "%d articles are marked as %s"
7974                   count (if unmark "unread" "read"))))
7975
7976 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7977   "Mark articles with same SUBJECT as read, and return marked number.
7978 If optional argument UNMARK is positive, remove any kinds of marks.
7979 If optional argument UNMARK is negative, mark articles as unread instead."
7980   (let ((count 1))
7981     (save-excursion
7982       (cond
7983        ((null unmark)                   ; Mark as read.
7984         (while (and
7985                 (progn
7986                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7987                   (gnus-summary-show-thread) t)
7988                 (gnus-summary-find-subject subject))
7989           (setq count (1+ count))))
7990        ((> unmark 0)                    ; Tick.
7991         (while (and
7992                 (progn
7993                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7994                   (gnus-summary-show-thread) t)
7995                 (gnus-summary-find-subject subject))
7996           (setq count (1+ count))))
7997        (t                               ; Mark as unread.
7998         (while (and
7999                 (progn
8000                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8001                   (gnus-summary-show-thread) t)
8002                 (gnus-summary-find-subject subject))
8003           (setq count (1+ count)))))
8004       (gnus-set-mode-line 'summary)
8005       ;; Return the number of marked articles.
8006       count)))
8007
8008 (defun gnus-summary-mark-as-processable (n &optional unmark)
8009   "Set the process mark on the next N articles.
8010 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8011 the process mark instead.  The difference between N and the actual
8012 number of articles marked is returned."
8013   (interactive "p")
8014   (let ((backward (< n 0))
8015         (n (abs n)))
8016     (while (and
8017             (> n 0)
8018             (if unmark
8019                 (gnus-summary-remove-process-mark
8020                  (gnus-summary-article-number))
8021               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8022             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8023       (setq n (1- n)))
8024     (when (/= 0 n)
8025       (gnus-message 7 "No more articles"))
8026     (gnus-summary-recenter)
8027     (gnus-summary-position-point)
8028     n))
8029
8030 (defun gnus-summary-unmark-as-processable (n)
8031   "Remove the process mark from the next N articles.
8032 If N is negative, unmark backward instead.  The difference between N and
8033 the actual number of articles unmarked is returned."
8034   (interactive "p")
8035   (gnus-summary-mark-as-processable n t))
8036
8037 (defun gnus-summary-unmark-all-processable ()
8038   "Remove the process mark from all articles."
8039   (interactive)
8040   (save-excursion
8041     (while gnus-newsgroup-processable
8042       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8043   (gnus-summary-position-point))
8044
8045 (defun gnus-summary-mark-as-expirable (n)
8046   "Mark N articles forward as expirable.
8047 If N is negative, mark backward instead.  The difference between N and
8048 the actual number of articles marked is returned."
8049   (interactive "p")
8050   (gnus-summary-mark-forward n gnus-expirable-mark))
8051
8052 (defun gnus-summary-mark-article-as-replied (article)
8053   "Mark ARTICLE replied and update the summary line."
8054   (push article gnus-newsgroup-replied)
8055   (let ((buffer-read-only nil))
8056     (when (gnus-summary-goto-subject article nil t)
8057       (gnus-summary-update-secondary-mark article))))
8058
8059 (defun gnus-summary-set-bookmark (article)
8060   "Set a bookmark in current article."
8061   (interactive (list (gnus-summary-article-number)))
8062   (when (or (not (get-buffer gnus-article-buffer))
8063             (not gnus-current-article)
8064             (not gnus-article-current)
8065             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8066     (error "No current article selected"))
8067   ;; Remove old bookmark, if one exists.
8068   (let ((old (assq article gnus-newsgroup-bookmarks)))
8069     (when old
8070       (setq gnus-newsgroup-bookmarks
8071             (delq old gnus-newsgroup-bookmarks))))
8072   ;; Set the new bookmark, which is on the form
8073   ;; (article-number . line-number-in-body).
8074   (push
8075    (cons article
8076          (save-excursion
8077            (set-buffer gnus-article-buffer)
8078            (count-lines
8079             (min (point)
8080                  (save-excursion
8081                    (goto-char (point-min))
8082                    (search-forward "\n\n" nil t)
8083                    (point)))
8084             (point))))
8085    gnus-newsgroup-bookmarks)
8086   (gnus-message 6 "A bookmark has been added to the current article."))
8087
8088 (defun gnus-summary-remove-bookmark (article)
8089   "Remove the bookmark from the current article."
8090   (interactive (list (gnus-summary-article-number)))
8091   ;; Remove old bookmark, if one exists.
8092   (let ((old (assq article gnus-newsgroup-bookmarks)))
8093     (if old
8094         (progn
8095           (setq gnus-newsgroup-bookmarks
8096                 (delq old gnus-newsgroup-bookmarks))
8097           (gnus-message 6 "Removed bookmark."))
8098       (gnus-message 6 "No bookmark in current article."))))
8099
8100 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8101 (defun gnus-summary-mark-as-dormant (n)
8102   "Mark N articles forward as dormant.
8103 If N is negative, mark backward instead.  The difference between N and
8104 the actual number of articles marked is returned."
8105   (interactive "p")
8106   (gnus-summary-mark-forward n gnus-dormant-mark))
8107
8108 (defun gnus-summary-set-process-mark (article)
8109   "Set the process mark on ARTICLE and update the summary line."
8110   (setq gnus-newsgroup-processable
8111         (cons article
8112               (delq article gnus-newsgroup-processable)))
8113   (when (gnus-summary-goto-subject article)
8114     (gnus-summary-show-thread)
8115     (gnus-summary-goto-subject article)
8116     (gnus-summary-update-secondary-mark article)))
8117
8118 (defun gnus-summary-remove-process-mark (article)
8119   "Remove the process mark from ARTICLE and update the summary line."
8120   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8121   (when (gnus-summary-goto-subject article)
8122     (gnus-summary-show-thread)
8123     (gnus-summary-goto-subject article)
8124     (gnus-summary-update-secondary-mark article)))
8125
8126 (defun gnus-summary-set-saved-mark (article)
8127   "Set the process mark on ARTICLE and update the summary line."
8128   (push article gnus-newsgroup-saved)
8129   (when (gnus-summary-goto-subject article)
8130     (gnus-summary-update-secondary-mark article)))
8131
8132 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8133   "Mark N articles as read forwards.
8134 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8135 The difference between N and the actual number of articles marked is
8136 returned.
8137 Iff NO-EXPIRE, auto-expiry will be inhibited."
8138   (interactive "p")
8139   (gnus-summary-show-thread)
8140   (let ((backward (< n 0))
8141         (gnus-summary-goto-unread
8142          (and gnus-summary-goto-unread
8143               (not (eq gnus-summary-goto-unread 'never))
8144               (not (memq mark (list gnus-unread-mark
8145                                     gnus-ticked-mark gnus-dormant-mark)))))
8146         (n (abs n))
8147         (mark (or mark gnus-del-mark)))
8148     (while (and (> n 0)
8149                 (gnus-summary-mark-article nil mark no-expire)
8150                 (zerop (gnus-summary-next-subject
8151                         (if backward -1 1)
8152                         (and gnus-summary-goto-unread
8153                              (not (eq gnus-summary-goto-unread 'never)))
8154                         t)))
8155       (setq n (1- n)))
8156     (when (/= 0 n)
8157       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8158     (gnus-summary-recenter)
8159     (gnus-summary-position-point)
8160     (gnus-set-mode-line 'summary)
8161     n))
8162
8163 (defun gnus-summary-mark-article-as-read (mark)
8164   "Mark the current article quickly as read with MARK."
8165   (let ((article (gnus-summary-article-number)))
8166     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8167     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8168     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8169     (push (cons article mark) gnus-newsgroup-reads)
8170     ;; Possibly remove from cache, if that is used.
8171     (when gnus-use-cache
8172       (gnus-cache-enter-remove-article article))
8173     ;; Allow the backend to change the mark.
8174     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8175     ;; Check for auto-expiry.
8176     (when (and gnus-newsgroup-auto-expire
8177                (memq mark gnus-auto-expirable-marks))
8178       (setq mark gnus-expirable-mark)
8179       ;; Let the backend know about the mark change.
8180       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8181       (push article gnus-newsgroup-expirable))
8182     ;; Set the mark in the buffer.
8183     (gnus-summary-update-mark mark 'unread)
8184     t))
8185
8186 (defun gnus-summary-mark-article-as-unread (mark)
8187   "Mark the current article quickly as unread with MARK."
8188   (let* ((article (gnus-summary-article-number))
8189          (old-mark (gnus-summary-article-mark article)))
8190     ;; Allow the backend to change the mark.
8191     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8192     (if (eq mark old-mark)
8193         t
8194       (if (<= article 0)
8195           (progn
8196             (gnus-error 1 "Can't mark negative article numbers")
8197             nil)
8198         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8199         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8200         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8201         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8202         (cond ((= mark gnus-ticked-mark)
8203                (push article gnus-newsgroup-marked))
8204               ((= mark gnus-dormant-mark)
8205                (push article gnus-newsgroup-dormant))
8206               (t
8207                (push article gnus-newsgroup-unreads)))
8208         (gnus-pull article gnus-newsgroup-reads)
8209
8210         ;; See whether the article is to be put in the cache.
8211         (and gnus-use-cache
8212              (vectorp (gnus-summary-article-header article))
8213              (save-excursion
8214                (gnus-cache-possibly-enter-article
8215                 gnus-newsgroup-name article
8216                 (= mark gnus-ticked-mark)
8217                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8218
8219         ;; Fix the mark.
8220         (gnus-summary-update-mark mark 'unread)
8221         t))))
8222
8223 (defun gnus-summary-mark-article (&optional article mark no-expire)
8224   "Mark ARTICLE with MARK.  MARK can be any character.
8225 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8226 `??' (dormant) and `?E' (expirable).
8227 If MARK is nil, then the default character `?r' is used.
8228 If ARTICLE is nil, then the article on the current line will be
8229 marked.
8230 Iff NO-EXPIRE, auto-expiry will be inhibited."
8231   ;; The mark might be a string.
8232   (when (stringp mark)
8233     (setq mark (aref mark 0)))
8234   ;; If no mark is given, then we check auto-expiring.
8235   (when (null mark)
8236     (setq mark gnus-del-mark))
8237   (when (and (not no-expire)
8238              gnus-newsgroup-auto-expire
8239              (memq mark gnus-auto-expirable-marks))
8240     (setq mark gnus-expirable-mark))
8241   (let ((article (or article (gnus-summary-article-number)))
8242         (old-mark (gnus-summary-article-mark article)))
8243     ;; Allow the backend to change the mark.
8244     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8245     (if (eq mark old-mark)
8246         t
8247       (unless article
8248         (error "No article on current line"))
8249       (if (not (if (or (= mark gnus-unread-mark)
8250                        (= mark gnus-ticked-mark)
8251                        (= mark gnus-dormant-mark))
8252                    (gnus-mark-article-as-unread article mark)
8253                  (gnus-mark-article-as-read article mark)))
8254           t
8255         ;; See whether the article is to be put in the cache.
8256         (and gnus-use-cache
8257              (not (= mark gnus-canceled-mark))
8258              (vectorp (gnus-summary-article-header article))
8259              (save-excursion
8260                (gnus-cache-possibly-enter-article
8261                 gnus-newsgroup-name article
8262                 (= mark gnus-ticked-mark)
8263                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8264
8265         (when (gnus-summary-goto-subject article nil t)
8266           (let ((buffer-read-only nil))
8267             (gnus-summary-show-thread)
8268             ;; Fix the mark.
8269             (gnus-summary-update-mark mark 'unread)
8270             t))))))
8271
8272 (defun gnus-summary-update-secondary-mark (article)
8273   "Update the secondary (read, process, cache) mark."
8274   (gnus-summary-update-mark
8275    (cond ((memq article gnus-newsgroup-processable)
8276           gnus-process-mark)
8277          ((memq article gnus-newsgroup-cached)
8278           gnus-cached-mark)
8279          ((memq article gnus-newsgroup-replied)
8280           gnus-replied-mark)
8281          ((memq article gnus-newsgroup-saved)
8282           gnus-saved-mark)
8283          (t gnus-unread-mark))
8284    'replied)
8285   (when (gnus-visual-p 'summary-highlight 'highlight)
8286     (gnus-run-hooks 'gnus-summary-update-hook))
8287   t)
8288
8289 (defun gnus-summary-update-mark (mark type)
8290   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8291         (buffer-read-only nil))
8292     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8293     (when forward
8294       (when (looking-at "\r")
8295         (incf forward))
8296       (when (<= (+ forward (point)) (point-max))
8297         ;; Go to the right position on the line.
8298         (goto-char (+ forward (point)))
8299         ;; Replace the old mark with the new mark.
8300         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8301         ;; Optionally update the marks by some user rule.
8302         (when (eq type 'unread)
8303           (gnus-data-set-mark
8304            (gnus-data-find (gnus-summary-article-number)) mark)
8305           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8306
8307 (defun gnus-mark-article-as-read (article &optional mark)
8308   "Enter ARTICLE in the pertinent lists and remove it from others."
8309   ;; Make the article expirable.
8310   (let ((mark (or mark gnus-del-mark)))
8311     (if (= mark gnus-expirable-mark)
8312         (push article gnus-newsgroup-expirable)
8313       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8314     ;; Remove from unread and marked lists.
8315     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8316     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8317     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8318     (push (cons article mark) gnus-newsgroup-reads)
8319     ;; Possibly remove from cache, if that is used.
8320     (when gnus-use-cache
8321       (gnus-cache-enter-remove-article article))
8322     t))
8323
8324 (defun gnus-mark-article-as-unread (article &optional mark)
8325   "Enter ARTICLE in the pertinent lists and remove it from others."
8326   (let ((mark (or mark gnus-ticked-mark)))
8327     (if (<= article 0)
8328         (progn
8329           (gnus-error 1 "Can't mark negative article numbers")
8330           nil)
8331       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8332             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8333             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8334             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8335
8336       ;; Unsuppress duplicates?
8337       (when gnus-suppress-duplicates
8338         (gnus-dup-unsuppress-article article))
8339
8340       (cond ((= mark gnus-ticked-mark)
8341              (push article gnus-newsgroup-marked))
8342             ((= mark gnus-dormant-mark)
8343              (push article gnus-newsgroup-dormant))
8344             (t
8345              (push article gnus-newsgroup-unreads)))
8346       (gnus-pull article gnus-newsgroup-reads)
8347       t)))
8348
8349 (defalias 'gnus-summary-mark-as-unread-forward
8350   'gnus-summary-tick-article-forward)
8351 (make-obsolete 'gnus-summary-mark-as-unread-forward
8352                'gnus-summary-tick-article-forward)
8353 (defun gnus-summary-tick-article-forward (n)
8354   "Tick N articles forwards.
8355 If N is negative, tick backwards instead.
8356 The difference between N and the number of articles ticked is returned."
8357   (interactive "p")
8358   (gnus-summary-mark-forward n gnus-ticked-mark))
8359
8360 (defalias 'gnus-summary-mark-as-unread-backward
8361   'gnus-summary-tick-article-backward)
8362 (make-obsolete 'gnus-summary-mark-as-unread-backward
8363                'gnus-summary-tick-article-backward)
8364 (defun gnus-summary-tick-article-backward (n)
8365   "Tick N articles backwards.
8366 The difference between N and the number of articles ticked is returned."
8367   (interactive "p")
8368   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8369
8370 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8371 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8372 (defun gnus-summary-tick-article (&optional article clear-mark)
8373   "Mark current article as unread.
8374 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8375 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8376   (interactive)
8377   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8378                                        gnus-ticked-mark)))
8379
8380 (defun gnus-summary-mark-as-read-forward (n)
8381   "Mark N articles as read forwards.
8382 If N is negative, mark backwards instead.
8383 The difference between N and the actual number of articles marked is
8384 returned."
8385   (interactive "p")
8386   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8387
8388 (defun gnus-summary-mark-as-read-backward (n)
8389   "Mark the N articles as read backwards.
8390 The difference between N and the actual number of articles marked is
8391 returned."
8392   (interactive "p")
8393   (gnus-summary-mark-forward
8394    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8395
8396 (defun gnus-summary-mark-as-read (&optional article mark)
8397   "Mark current article as read.
8398 ARTICLE specifies the article to be marked as read.
8399 MARK specifies a string to be inserted at the beginning of the line."
8400   (gnus-summary-mark-article article mark))
8401
8402 (defun gnus-summary-clear-mark-forward (n)
8403   "Clear marks from N articles forward.
8404 If N is negative, clear backward instead.
8405 The difference between N and the number of marks cleared is returned."
8406   (interactive "p")
8407   (gnus-summary-mark-forward n gnus-unread-mark))
8408
8409 (defun gnus-summary-clear-mark-backward (n)
8410   "Clear marks from N articles backward.
8411 The difference between N and the number of marks cleared is returned."
8412   (interactive "p")
8413   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8414
8415 (defun gnus-summary-mark-unread-as-read ()
8416   "Intended to be used by `gnus-summary-mark-article-hook'."
8417   (when (memq gnus-current-article gnus-newsgroup-unreads)
8418     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8419
8420 (defun gnus-summary-mark-read-and-unread-as-read ()
8421   "Intended to be used by `gnus-summary-mark-article-hook'."
8422   (let ((mark (gnus-summary-article-mark)))
8423     (when (or (gnus-unread-mark-p mark)
8424               (gnus-read-mark-p mark))
8425       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8426
8427 (defun gnus-summary-mark-region-as-read (point mark all)
8428   "Mark all unread articles between point and mark as read.
8429 If given a prefix, mark all articles between point and mark as read,
8430 even ticked and dormant ones."
8431   (interactive "r\nP")
8432   (save-excursion
8433     (let (article)
8434       (goto-char point)
8435       (beginning-of-line)
8436       (while (and
8437               (< (point) mark)
8438               (progn
8439                 (when (or all
8440                           (memq (setq article (gnus-summary-article-number))
8441                                 gnus-newsgroup-unreads))
8442                   (gnus-summary-mark-article article gnus-del-mark))
8443                 t)
8444               (gnus-summary-find-next))))))
8445
8446 (defun gnus-summary-mark-below (score mark)
8447   "Mark articles with score less than SCORE with MARK."
8448   (interactive "P\ncMark: ")
8449   (setq score (if score
8450                   (prefix-numeric-value score)
8451                 (or gnus-summary-default-score 0)))
8452   (save-excursion
8453     (set-buffer gnus-summary-buffer)
8454     (goto-char (point-min))
8455     (while
8456         (progn
8457           (and (< (gnus-summary-article-score) score)
8458                (gnus-summary-mark-article nil mark))
8459           (gnus-summary-find-next)))))
8460
8461 (defun gnus-summary-kill-below (&optional score)
8462   "Mark articles with score below SCORE as read."
8463   (interactive "P")
8464   (gnus-summary-mark-below score gnus-killed-mark))
8465
8466 (defun gnus-summary-clear-above (&optional score)
8467   "Clear all marks from articles with score above SCORE."
8468   (interactive "P")
8469   (gnus-summary-mark-above score gnus-unread-mark))
8470
8471 (defun gnus-summary-tick-above (&optional score)
8472   "Tick all articles with score above SCORE."
8473   (interactive "P")
8474   (gnus-summary-mark-above score gnus-ticked-mark))
8475
8476 (defun gnus-summary-mark-above (score mark)
8477   "Mark articles with score over SCORE with MARK."
8478   (interactive "P\ncMark: ")
8479   (setq score (if score
8480                   (prefix-numeric-value score)
8481                 (or gnus-summary-default-score 0)))
8482   (save-excursion
8483     (set-buffer gnus-summary-buffer)
8484     (goto-char (point-min))
8485     (while (and (progn
8486                   (when (> (gnus-summary-article-score) score)
8487                     (gnus-summary-mark-article nil mark))
8488                   t)
8489                 (gnus-summary-find-next)))))
8490
8491 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8492 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8493 (defun gnus-summary-limit-include-expunged (&optional no-error)
8494   "Display all the hidden articles that were expunged for low scores."
8495   (interactive)
8496   (let ((buffer-read-only nil))
8497     (let ((scored gnus-newsgroup-scored)
8498           headers h)
8499       (while scored
8500         (unless (gnus-summary-goto-subject (caar scored))
8501           (and (setq h (gnus-summary-article-header (caar scored)))
8502                (< (cdar scored) gnus-summary-expunge-below)
8503                (push h headers)))
8504         (setq scored (cdr scored)))
8505       (if (not headers)
8506           (when (not no-error)
8507             (error "No expunged articles hidden"))
8508         (goto-char (point-min))
8509         (gnus-summary-prepare-unthreaded (nreverse headers))
8510         (goto-char (point-min))
8511         (gnus-summary-position-point)
8512         t))))
8513
8514 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8515   "Mark all unread articles in this newsgroup as read.
8516 If prefix argument ALL is non-nil, ticked and dormant articles will
8517 also be marked as read.
8518 If QUIETLY is non-nil, no questions will be asked.
8519 If TO-HERE is non-nil, it should be a point in the buffer.  All
8520 articles before this point will be marked as read.
8521 Note that this function will only catch up the unread article
8522 in the current summary buffer limitation.
8523 The number of articles marked as read is returned."
8524   (interactive "P")
8525   (prog1
8526       (save-excursion
8527         (when (or quietly
8528                   (not gnus-interactive-catchup) ;Without confirmation?
8529                   gnus-expert-user
8530                   (gnus-y-or-n-p
8531                    (if all
8532                        "Mark absolutely all articles as read? "
8533                      "Mark all unread articles as read? ")))
8534           (if (and not-mark
8535                    (not gnus-newsgroup-adaptive)
8536                    (not gnus-newsgroup-auto-expire)
8537                    (not gnus-suppress-duplicates)
8538                    (or (not gnus-use-cache)
8539                        (eq gnus-use-cache 'passive)))
8540               (progn
8541                 (when all
8542                   (setq gnus-newsgroup-marked nil
8543                         gnus-newsgroup-dormant nil))
8544                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8545             ;; We actually mark all articles as canceled, which we
8546             ;; have to do when using auto-expiry or adaptive scoring.
8547             (gnus-summary-show-all-threads)
8548             (when (gnus-summary-first-subject (not all) t)
8549               (while (and
8550                       (if to-here (< (point) to-here) t)
8551                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8552                       (gnus-summary-find-next (not all) nil nil t))))
8553             (gnus-set-mode-line 'summary))
8554           t))
8555     (gnus-summary-position-point)))
8556
8557 (defun gnus-summary-catchup-to-here (&optional all)
8558   "Mark all unticked articles before the current one as read.
8559 If ALL is non-nil, also mark ticked and dormant articles as read."
8560   (interactive "P")
8561   (save-excursion
8562     (gnus-save-hidden-threads
8563       (let ((beg (point)))
8564         ;; We check that there are unread articles.
8565         (when (or all (gnus-summary-find-prev))
8566           (gnus-summary-catchup all t beg)))))
8567   (gnus-summary-position-point))
8568
8569 (defun gnus-summary-catchup-all (&optional quietly)
8570   "Mark all articles in this newsgroup as read."
8571   (interactive "P")
8572   (gnus-summary-catchup t quietly))
8573
8574 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8575   "Mark all unread articles in this group as read, then exit.
8576 If prefix argument ALL is non-nil, all articles are marked as read."
8577   (interactive "P")
8578   (when (gnus-summary-catchup all quietly nil 'fast)
8579     ;; Select next newsgroup or exit.
8580     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8581              (eq gnus-auto-select-next 'quietly))
8582         (gnus-summary-next-group nil)
8583       (gnus-summary-exit))))
8584
8585 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8586   "Mark all articles in this newsgroup as read, and then exit."
8587   (interactive "P")
8588   (gnus-summary-catchup-and-exit t quietly))
8589
8590 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8591   "Mark all articles in this group as read and select the next group.
8592 If given a prefix, mark all articles, unread as well as ticked, as
8593 read."
8594   (interactive "P")
8595   (save-excursion
8596     (gnus-summary-catchup all))
8597   (gnus-summary-next-group))
8598
8599 ;;;
8600 ;;; with article
8601 ;;;
8602
8603 (defmacro gnus-with-article (article &rest forms)
8604   "Select ARTICLE and perform FORMS in the original article buffer.
8605 Then replace the article with the result."
8606   `(progn
8607      ;; We don't want the article to be marked as read.
8608      (let (gnus-mark-article-hook)
8609        (gnus-summary-select-article t t nil ,article))
8610      (set-buffer gnus-original-article-buffer)
8611      ,@forms
8612      (if (not (gnus-check-backend-function
8613                'request-replace-article (car gnus-article-current)))
8614          (gnus-message 5 "Read-only group; not replacing")
8615        (unless (gnus-request-replace-article
8616                 ,article (car gnus-article-current)
8617                 (current-buffer) t)
8618          (error "Couldn't replace article")))
8619      ;; The cache and backlog have to be flushed somewhat.
8620      (when gnus-keep-backlog
8621        (gnus-backlog-remove-article
8622         (car gnus-article-current) (cdr gnus-article-current)))
8623      (when gnus-use-cache
8624        (gnus-cache-update-article
8625         (car gnus-article-current) (cdr gnus-article-current)))))
8626
8627 (put 'gnus-with-article 'lisp-indent-function 1)
8628 (put 'gnus-with-article 'edebug-form-spec '(form body))
8629
8630 ;; Thread-based commands.
8631
8632 (defun gnus-summary-articles-in-thread (&optional article)
8633   "Return a list of all articles in the current thread.
8634 If ARTICLE is non-nil, return all articles in the thread that starts
8635 with that article."
8636   (let* ((article (or article (gnus-summary-article-number)))
8637          (data (gnus-data-find-list article))
8638          (top-level (gnus-data-level (car data)))
8639          (top-subject
8640           (cond ((null gnus-thread-operation-ignore-subject)
8641                  (gnus-simplify-subject-re
8642                   (mail-header-subject (gnus-data-header (car data)))))
8643                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8644                  (gnus-simplify-subject-fuzzy
8645                   (mail-header-subject (gnus-data-header (car data)))))
8646                 (t nil)))
8647          (end-point (save-excursion
8648                       (if (gnus-summary-go-to-next-thread)
8649                           (point) (point-max))))
8650          articles)
8651     (while (and data
8652                 (< (gnus-data-pos (car data)) end-point))
8653       (when (or (not top-subject)
8654                 (string= top-subject
8655                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8656                              (gnus-simplify-subject-fuzzy
8657                               (mail-header-subject
8658                                (gnus-data-header (car data))))
8659                            (gnus-simplify-subject-re
8660                             (mail-header-subject
8661                              (gnus-data-header (car data)))))))
8662         (push (gnus-data-number (car data)) articles))
8663       (unless (and (setq data (cdr data))
8664                    (> (gnus-data-level (car data)) top-level))
8665         (setq data nil)))
8666     ;; Return the list of articles.
8667     (nreverse articles)))
8668
8669 (defun gnus-summary-rethread-current ()
8670   "Rethread the thread the current article is part of."
8671   (interactive)
8672   (let* ((gnus-show-threads t)
8673          (article (gnus-summary-article-number))
8674          (id (mail-header-id (gnus-summary-article-header)))
8675          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8676     (unless id
8677       (error "No article on the current line"))
8678     (gnus-rebuild-thread id)
8679     (gnus-summary-goto-subject article)))
8680
8681 (defun gnus-summary-reparent-thread ()
8682   "Make the current article child of the marked (or previous) article.
8683
8684 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8685 is non-nil or the Subject: of both articles are the same."
8686   (interactive)
8687   (unless (not (gnus-group-read-only-p))
8688     (error "The current newsgroup does not support article editing"))
8689   (unless (<= (length gnus-newsgroup-processable) 1)
8690     (error "No more than one article may be marked"))
8691   (save-window-excursion
8692     (let ((gnus-article-buffer " *reparent*")
8693           (current-article (gnus-summary-article-number))
8694           ;; First grab the marked article, otherwise one line up.
8695           (parent-article (if (not (null gnus-newsgroup-processable))
8696                               (car gnus-newsgroup-processable)
8697                             (save-excursion
8698                               (if (eq (forward-line -1) 0)
8699                                   (gnus-summary-article-number)
8700                                 (error "Beginning of summary buffer"))))))
8701       (unless (not (eq current-article parent-article))
8702         (error "An article may not be self-referential"))
8703       (let ((message-id (mail-header-id
8704                          (gnus-summary-article-header parent-article))))
8705         (unless (and message-id (not (equal message-id "")))
8706           (error "No message-id in desired parent"))
8707         (gnus-with-article current-article
8708           (save-restriction
8709             (goto-char (point-min))
8710             (message-narrow-to-head)
8711             (if (re-search-forward "^References: " nil t)
8712                 (progn
8713                   (re-search-forward "^[^ \t]" nil t)
8714                   (forward-line -1)
8715                   (end-of-line)
8716                   (insert " " message-id))
8717               (insert "References: " message-id "\n"))))
8718         (set-buffer gnus-summary-buffer)
8719         (gnus-summary-unmark-all-processable)
8720         (gnus-summary-update-article current-article)
8721         (gnus-summary-rethread-current)
8722         (gnus-message 3 "Article %d is now the child of article %d"
8723                       current-article parent-article)))))
8724
8725 (defun gnus-summary-toggle-threads (&optional arg)
8726   "Toggle showing conversation threads.
8727 If ARG is positive number, turn showing conversation threads on."
8728   (interactive "P")
8729   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8730     (setq gnus-show-threads
8731           (if (null arg) (not gnus-show-threads)
8732             (> (prefix-numeric-value arg) 0)))
8733     (gnus-summary-prepare)
8734     (gnus-summary-goto-subject current)
8735     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8736     (gnus-summary-position-point)))
8737
8738 (defun gnus-summary-show-all-threads ()
8739   "Show all threads."
8740   (interactive)
8741   (save-excursion
8742     (let ((buffer-read-only nil))
8743       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8744   (gnus-summary-position-point))
8745
8746 (defun gnus-summary-show-thread ()
8747   "Show thread subtrees.
8748 Returns nil if no thread was there to be shown."
8749   (interactive)
8750   (let ((buffer-read-only nil)
8751         (orig (point))
8752         ;; first goto end then to beg, to have point at beg after let
8753         (end (progn (end-of-line) (point)))
8754         (beg (progn (beginning-of-line) (point))))
8755     (prog1
8756         ;; Any hidden lines here?
8757         (search-forward "\r" end t)
8758       (subst-char-in-region beg end ?\^M ?\n t)
8759       (goto-char orig)
8760       (gnus-summary-position-point))))
8761
8762 (defun gnus-summary-hide-all-threads ()
8763   "Hide all thread subtrees."
8764   (interactive)
8765   (save-excursion
8766     (goto-char (point-min))
8767     (gnus-summary-hide-thread)
8768     (while (zerop (gnus-summary-next-thread 1 t))
8769       (gnus-summary-hide-thread)))
8770   (gnus-summary-position-point))
8771
8772 (defun gnus-summary-hide-thread ()
8773   "Hide thread subtrees.
8774 Returns nil if no threads were there to be hidden."
8775   (interactive)
8776   (let ((buffer-read-only nil)
8777         (start (point))
8778         (article (gnus-summary-article-number)))
8779     (goto-char start)
8780     ;; Go forward until either the buffer ends or the subthread
8781     ;; ends.
8782     (when (and (not (eobp))
8783                (or (zerop (gnus-summary-next-thread 1 t))
8784                    (goto-char (point-max))))
8785       (prog1
8786           (if (and (> (point) start)
8787                    (search-backward "\n" start t))
8788               (progn
8789                 (subst-char-in-region start (point) ?\n ?\^M)
8790                 (gnus-summary-goto-subject article))
8791             (goto-char start)
8792             nil)))))
8793
8794 (defun gnus-summary-go-to-next-thread (&optional previous)
8795   "Go to the same level (or less) next thread.
8796 If PREVIOUS is non-nil, go to previous thread instead.
8797 Return the article number moved to, or nil if moving was impossible."
8798   (let ((level (gnus-summary-thread-level))
8799         (way (if previous -1 1))
8800         (beg (point)))
8801     (forward-line way)
8802     (while (and (not (eobp))
8803                 (< level (gnus-summary-thread-level)))
8804       (forward-line way))
8805     (if (eobp)
8806         (progn
8807           (goto-char beg)
8808           nil)
8809       (setq beg (point))
8810       (prog1
8811           (gnus-summary-article-number)
8812         (goto-char beg)))))
8813
8814 (defun gnus-summary-next-thread (n &optional silent)
8815   "Go to the same level next N'th thread.
8816 If N is negative, search backward instead.
8817 Returns the difference between N and the number of skips actually
8818 done.
8819
8820 If SILENT, don't output messages."
8821   (interactive "p")
8822   (let ((backward (< n 0))
8823         (n (abs n)))
8824     (while (and (> n 0)
8825                 (gnus-summary-go-to-next-thread backward))
8826       (decf n))
8827     (unless silent
8828       (gnus-summary-position-point))
8829     (when (and (not silent) (/= 0 n))
8830       (gnus-message 7 "No more threads"))
8831     n))
8832
8833 (defun gnus-summary-prev-thread (n)
8834   "Go to the same level previous N'th thread.
8835 Returns the difference between N and the number of skips actually
8836 done."
8837   (interactive "p")
8838   (gnus-summary-next-thread (- n)))
8839
8840 (defun gnus-summary-go-down-thread ()
8841   "Go down one level in the current thread."
8842   (let ((children (gnus-summary-article-children)))
8843     (when children
8844       (gnus-summary-goto-subject (car children)))))
8845
8846 (defun gnus-summary-go-up-thread ()
8847   "Go up one level in the current thread."
8848   (let ((parent (gnus-summary-article-parent)))
8849     (when parent
8850       (gnus-summary-goto-subject parent))))
8851
8852 (defun gnus-summary-down-thread (n)
8853   "Go down thread N steps.
8854 If N is negative, go up instead.
8855 Returns the difference between N and how many steps down that were
8856 taken."
8857   (interactive "p")
8858   (let ((up (< n 0))
8859         (n (abs n)))
8860     (while (and (> n 0)
8861                 (if up (gnus-summary-go-up-thread)
8862                   (gnus-summary-go-down-thread)))
8863       (setq n (1- n)))
8864     (gnus-summary-position-point)
8865     (when (/= 0 n)
8866       (gnus-message 7 "Can't go further"))
8867     n))
8868
8869 (defun gnus-summary-up-thread (n)
8870   "Go up thread N steps.
8871 If N is negative, go up instead.
8872 Returns the difference between N and how many steps down that were
8873 taken."
8874   (interactive "p")
8875   (gnus-summary-down-thread (- n)))
8876
8877 (defun gnus-summary-top-thread ()
8878   "Go to the top of the thread."
8879   (interactive)
8880   (while (gnus-summary-go-up-thread))
8881   (gnus-summary-article-number))
8882
8883 (defun gnus-summary-kill-thread (&optional unmark)
8884   "Mark articles under current thread as read.
8885 If the prefix argument is positive, remove any kinds of marks.
8886 If the prefix argument is negative, tick articles instead."
8887   (interactive "P")
8888   (when unmark
8889     (setq unmark (prefix-numeric-value unmark)))
8890   (let ((articles (gnus-summary-articles-in-thread)))
8891     (save-excursion
8892       ;; Expand the thread.
8893       (gnus-summary-show-thread)
8894       ;; Mark all the articles.
8895       (while articles
8896         (gnus-summary-goto-subject (car articles))
8897         (cond ((null unmark)
8898                (gnus-summary-mark-article-as-read gnus-killed-mark))
8899               ((> unmark 0)
8900                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8901               (t
8902                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8903         (setq articles (cdr articles))))
8904     ;; Hide killed subtrees.
8905     (and (null unmark)
8906          gnus-thread-hide-killed
8907          (gnus-summary-hide-thread))
8908     ;; If marked as read, go to next unread subject.
8909     (when (null unmark)
8910       ;; Go to next unread subject.
8911       (gnus-summary-next-subject 1 t)))
8912   (gnus-set-mode-line 'summary))
8913
8914 ;; Summary sorting commands
8915
8916 (defun gnus-summary-sort-by-number (&optional reverse)
8917   "Sort the summary buffer by article number.
8918 Argument REVERSE means reverse order."
8919   (interactive "P")
8920   (gnus-summary-sort 'number reverse))
8921
8922 (defun gnus-summary-sort-by-author (&optional reverse)
8923   "Sort the summary buffer by author name alphabetically.
8924 If `case-fold-search' is non-nil, case of letters is ignored.
8925 Argument REVERSE means reverse order."
8926   (interactive "P")
8927   (gnus-summary-sort 'author reverse))
8928
8929 (defun gnus-summary-sort-by-subject (&optional reverse)
8930   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8931 If `case-fold-search' is non-nil, case of letters is ignored.
8932 Argument REVERSE means reverse order."
8933   (interactive "P")
8934   (gnus-summary-sort 'subject reverse))
8935
8936 (defun gnus-summary-sort-by-date (&optional reverse)
8937   "Sort the summary buffer by date.
8938 Argument REVERSE means reverse order."
8939   (interactive "P")
8940   (gnus-summary-sort 'date reverse))
8941
8942 (defun gnus-summary-sort-by-score (&optional reverse)
8943   "Sort the summary buffer by score.
8944 Argument REVERSE means reverse order."
8945   (interactive "P")
8946   (gnus-summary-sort 'score reverse))
8947
8948 (defun gnus-summary-sort-by-lines (&optional reverse)
8949   "Sort the summary buffer by the number of lines.
8950 Argument REVERSE means reverse order."
8951   (interactive "P")
8952   (gnus-summary-sort 'lines reverse))
8953
8954 (defun gnus-summary-sort-by-chars (&optional reverse)
8955   "Sort the summary buffer by article length.
8956 Argument REVERSE means reverse order."
8957   (interactive "P")
8958   (gnus-summary-sort 'chars reverse))
8959
8960 (defun gnus-summary-sort (predicate reverse)
8961   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8962   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8963          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8964          (gnus-thread-sort-functions
8965           (if (not reverse)
8966               thread
8967             `(lambda (t1 t2)
8968                (,thread t2 t1))))
8969          (gnus-sort-gathered-threads-function
8970           gnus-thread-sort-functions)
8971          (gnus-article-sort-functions
8972           (if (not reverse)
8973               article
8974             `(lambda (t1 t2)
8975                (,article t2 t1))))
8976          (buffer-read-only)
8977          (gnus-summary-prepare-hook nil))
8978     ;; We do the sorting by regenerating the threads.
8979     (gnus-summary-prepare)
8980     ;; Hide subthreads if needed.
8981     (when (and gnus-show-threads gnus-thread-hide-subtree)
8982       (gnus-summary-hide-all-threads))))
8983
8984 ;; Summary saving commands.
8985
8986 (defun gnus-summary-save-article (&optional n not-saved)
8987   "Save the current article using the default saver function.
8988 If N is a positive number, save the N next articles.
8989 If N is a negative number, save the N previous articles.
8990 If N is nil and any articles have been marked with the process mark,
8991 save those articles instead.
8992 The variable `gnus-default-article-saver' specifies the saver function."
8993   (interactive "P")
8994   (let* ((articles (gnus-summary-work-articles n))
8995          (save-buffer (save-excursion
8996                         (nnheader-set-temp-buffer " *Gnus Save*")))
8997          (num (length articles))
8998          header file)
8999     (dolist (article articles)
9000       (setq header (gnus-summary-article-header article))
9001       (if (not (vectorp header))
9002           ;; This is a pseudo-article.
9003           (if (assq 'name header)
9004               (gnus-copy-file (cdr (assq 'name header)))
9005             (gnus-message 1 "Article %d is unsaveable" article))
9006         ;; This is a real article.
9007         (save-window-excursion
9008           (gnus-summary-select-article t nil nil article))
9009         (save-excursion
9010           (set-buffer save-buffer)
9011           (erase-buffer)
9012           (insert-buffer-substring gnus-original-article-buffer))
9013         (setq file (gnus-article-save save-buffer file num))
9014         (gnus-summary-remove-process-mark article)
9015         (unless not-saved
9016           (gnus-summary-set-saved-mark article))))
9017     (gnus-kill-buffer save-buffer)
9018     (gnus-summary-position-point)
9019     (gnus-set-mode-line 'summary)
9020     n))
9021
9022 (defun gnus-summary-pipe-output (&optional arg)
9023   "Pipe the current article to a subprocess.
9024 If N is a positive number, pipe the N next articles.
9025 If N is a negative number, pipe the N previous articles.
9026 If N is nil and any articles have been marked with the process mark,
9027 pipe those articles instead."
9028   (interactive "P")
9029   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9030     (gnus-summary-save-article arg t))
9031   (gnus-configure-windows 'pipe))
9032
9033 (defun gnus-summary-save-article-mail (&optional arg)
9034   "Append the current article to an mail file.
9035 If N is a positive number, save the N next articles.
9036 If N is a negative number, save the N previous articles.
9037 If N is nil and any articles have been marked with the process mark,
9038 save those articles instead."
9039   (interactive "P")
9040   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9041     (gnus-summary-save-article arg)))
9042
9043 (defun gnus-summary-save-article-rmail (&optional arg)
9044   "Append the current article to an rmail file.
9045 If N is a positive number, save the N next articles.
9046 If N is a negative number, save the N previous articles.
9047 If N is nil and any articles have been marked with the process mark,
9048 save those articles instead."
9049   (interactive "P")
9050   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9051     (gnus-summary-save-article arg)))
9052
9053 (defun gnus-summary-save-article-file (&optional arg)
9054   "Append the current article to a file.
9055 If N is a positive number, save the N next articles.
9056 If N is a negative number, save the N previous articles.
9057 If N is nil and any articles have been marked with the process mark,
9058 save those articles instead."
9059   (interactive "P")
9060   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9061     (gnus-summary-save-article arg)))
9062
9063 (defun gnus-summary-write-article-file (&optional arg)
9064   "Write the current article to a file, deleting the previous file.
9065 If N is a positive number, save the N next articles.
9066 If N is a negative number, save the N previous articles.
9067 If N is nil and any articles have been marked with the process mark,
9068 save those articles instead."
9069   (interactive "P")
9070   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9071     (gnus-summary-save-article arg)))
9072
9073 (defun gnus-summary-save-article-body-file (&optional arg)
9074   "Append the current article body to a file.
9075 If N is a positive number, save the N next articles.
9076 If N is a negative number, save the N previous articles.
9077 If N is nil and any articles have been marked with the process mark,
9078 save those articles instead."
9079   (interactive "P")
9080   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9081     (gnus-summary-save-article arg)))
9082
9083 (defun gnus-summary-pipe-message (program)
9084   "Pipe the current article through PROGRAM."
9085   (interactive "sProgram: ")
9086   (gnus-summary-select-article)
9087   (let ((mail-header-separator ""))
9088     (gnus-eval-in-buffer-window gnus-article-buffer
9089       (save-restriction
9090         (widen)
9091         (let ((start (window-start))
9092               buffer-read-only)
9093           (message-pipe-buffer-body program)
9094           (set-window-start (get-buffer-window (current-buffer)) start))))))
9095
9096 (defun gnus-get-split-value (methods)
9097   "Return a value based on the split METHODS."
9098   (let (split-name method result match)
9099     (when methods
9100       (save-excursion
9101         (set-buffer gnus-original-article-buffer)
9102         (save-restriction
9103           (nnheader-narrow-to-headers)
9104           (while methods
9105             (goto-char (point-min))
9106             (setq method (pop methods))
9107             (setq match (car method))
9108             (when (cond
9109                    ((stringp match)
9110                     ;; Regular expression.
9111                     (ignore-errors
9112                       (re-search-forward match nil t)))
9113                    ((gnus-functionp match)
9114                     ;; Function.
9115                     (save-restriction
9116                       (widen)
9117                       (setq result (funcall match gnus-newsgroup-name))))
9118                    ((consp match)
9119                     ;; Form.
9120                     (save-restriction
9121                       (widen)
9122                       (setq result (eval match)))))
9123               (setq split-name (append (cdr method) split-name))
9124               (cond ((stringp result)
9125                      (push (expand-file-name
9126                             result gnus-article-save-directory)
9127                            split-name))
9128                     ((consp result)
9129                      (setq split-name (append result split-name)))))))))
9130     (nreverse split-name)))
9131
9132 (defun gnus-valid-move-group-p (group)
9133   (and (boundp group)
9134        (symbol-name group)
9135        (symbol-value group)
9136        (gnus-get-function (gnus-find-method-for-group
9137                            (symbol-name group)) 'request-accept-article t)))
9138
9139 (defun gnus-read-move-group-name (prompt default articles prefix)
9140   "Read a group name."
9141   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9142          (minibuffer-confirm-incomplete nil) ; XEmacs
9143          (prom
9144           (format "%s %s to:"
9145                   prompt
9146                   (if (> (length articles) 1)
9147                       (format "these %d articles" (length articles))
9148                     "this article")))
9149          (to-newsgroup
9150           (cond
9151            ((null split-name)
9152             (gnus-completing-read default prom
9153                                   gnus-active-hashtb
9154                                   'gnus-valid-move-group-p
9155                                   nil prefix
9156                                   'gnus-group-history))
9157            ((= 1 (length split-name))
9158             (gnus-completing-read (car split-name) prom
9159                                   gnus-active-hashtb
9160                                   'gnus-valid-move-group-p
9161                                   nil nil
9162                                   'gnus-group-history))
9163            (t
9164             (gnus-completing-read nil prom
9165                                   (mapcar (lambda (el) (list el))
9166                                           (nreverse split-name))
9167                                   nil nil nil
9168                                   'gnus-group-history))))
9169          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9170     (when to-newsgroup
9171       (if (or (string= to-newsgroup "")
9172               (string= to-newsgroup prefix))
9173           (setq to-newsgroup default))
9174       (unless to-newsgroup
9175         (error "No group name entered"))
9176       (or (gnus-active to-newsgroup)
9177           (gnus-activate-group to-newsgroup nil nil to-method)
9178           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9179                                      to-newsgroup))
9180               (or (and (gnus-request-create-group to-newsgroup to-method)
9181                        (gnus-activate-group
9182                         to-newsgroup nil nil to-method)
9183                        (gnus-subscribe-group to-newsgroup))
9184                   (error "Couldn't create group %s" to-newsgroup)))
9185           (error "No such group: %s" to-newsgroup)))
9186     to-newsgroup))
9187
9188 (defun gnus-summary-save-parts (type dir n &optional reverse)
9189   "Save parts matching TYPE to DIR.
9190 If REVERSE, save parts that do not match TYPE."
9191   (interactive
9192    (list (read-string "Save parts of type: " "image/.*")
9193          (read-file-name "Save to directory: " nil nil t)
9194          current-prefix-arg))
9195   (gnus-summary-iterate n
9196     (let ((gnus-display-mime-function nil)
9197           (gnus-inhibit-treatment t))
9198       (gnus-summary-select-article))
9199     (save-excursion
9200       (set-buffer gnus-article-buffer)
9201       (let ((handles (or gnus-article-mime-handles
9202                          (mm-dissect-buffer) (mm-uu-dissect))))
9203         (when handles
9204           (gnus-summary-save-parts-1 type dir handles reverse)
9205           (unless gnus-article-mime-handles ;; Don't destroy this case.
9206             (mm-destroy-parts handles)))))))
9207
9208 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9209   (if (stringp (car handle))
9210       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9211               (cdr handle))
9212     (when (if reverse
9213               (not (string-match type (mm-handle-media-type handle)))
9214             (string-match type (mm-handle-media-type handle)))
9215       (let ((file (expand-file-name
9216                    (file-name-nondirectory
9217                     (or
9218                      (mail-content-type-get
9219                       (mm-handle-disposition handle) 'filename)
9220                      (concat gnus-newsgroup-name
9221                              "." (number-to-string
9222                                   (cdr gnus-article-current)))))
9223                    dir)))
9224         (unless (file-exists-p file)
9225           (mm-save-part-to-file handle file))))))
9226
9227 ;; Summary extract commands
9228
9229 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9230   (let ((buffer-read-only nil)
9231         (article (gnus-summary-article-number))
9232         after-article b e)
9233     (unless (gnus-summary-goto-subject article)
9234       (error "No such article: %d" article))
9235     (gnus-summary-position-point)
9236     ;; If all commands are to be bunched up on one line, we collect
9237     ;; them here.
9238     (unless gnus-view-pseudos-separately
9239       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9240             files action)
9241         (while ps
9242           (setq action (cdr (assq 'action (car ps))))
9243           (setq files (list (cdr (assq 'name (car ps)))))
9244           (while (and ps (cdr ps)
9245                       (string= (or action "1")
9246                                (or (cdr (assq 'action (cadr ps))) "2")))
9247             (push (cdr (assq 'name (cadr ps))) files)
9248             (setcdr ps (cddr ps)))
9249           (when files
9250             (when (not (string-match "%s" action))
9251               (push " " files))
9252             (push " " files)
9253             (when (assq 'execute (car ps))
9254               (setcdr (assq 'execute (car ps))
9255                       (funcall (if (string-match "%s" action)
9256                                    'format 'concat)
9257                                action
9258                                (mapconcat
9259                                 (lambda (f)
9260                                   (if (equal f " ")
9261                                       f
9262                                     (mm-quote-arg f)))
9263                                 files " ")))))
9264           (setq ps (cdr ps)))))
9265     (if (and gnus-view-pseudos (not not-view))
9266         (while pslist
9267           (when (assq 'execute (car pslist))
9268             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9269                                   (eq gnus-view-pseudos 'not-confirm)))
9270           (setq pslist (cdr pslist)))
9271       (save-excursion
9272         (while pslist
9273           (setq after-article (or (cdr (assq 'article (car pslist)))
9274                                   (gnus-summary-article-number)))
9275           (gnus-summary-goto-subject after-article)
9276           (forward-line 1)
9277           (setq b (point))
9278           (insert "    " (file-name-nondirectory
9279                           (cdr (assq 'name (car pslist))))
9280                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9281           (setq e (point))
9282           (forward-line -1)             ; back to `b'
9283           (gnus-add-text-properties
9284            b (1- e) (list 'gnus-number gnus-reffed-article-number
9285                           gnus-mouse-face-prop gnus-mouse-face))
9286           (gnus-data-enter
9287            after-article gnus-reffed-article-number
9288            gnus-unread-mark b (car pslist) 0 (- e b))
9289           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9290           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9291           (setq pslist (cdr pslist)))))))
9292
9293 (defun gnus-pseudos< (p1 p2)
9294   (let ((c1 (cdr (assq 'action p1)))
9295         (c2 (cdr (assq 'action p2))))
9296     (and c1 c2 (string< c1 c2))))
9297
9298 (defun gnus-request-pseudo-article (props)
9299   (cond ((assq 'execute props)
9300          (gnus-execute-command (cdr (assq 'execute props)))))
9301   (let ((gnus-current-article (gnus-summary-article-number)))
9302     (gnus-run-hooks 'gnus-mark-article-hook)))
9303
9304 (defun gnus-execute-command (command &optional automatic)
9305   (save-excursion
9306     (gnus-article-setup-buffer)
9307     (set-buffer gnus-article-buffer)
9308     (setq buffer-read-only nil)
9309     (let ((command (if automatic command
9310                      (read-string "Command: " (cons command 0)))))
9311       (erase-buffer)
9312       (insert "$ " command "\n\n")
9313       (if gnus-view-pseudo-asynchronously
9314           (start-process "gnus-execute" (current-buffer) shell-file-name
9315                          shell-command-switch command)
9316         (call-process shell-file-name nil t nil
9317                       shell-command-switch command)))))
9318
9319 ;; Summary kill commands.
9320
9321 (defun gnus-summary-edit-global-kill (article)
9322   "Edit the \"global\" kill file."
9323   (interactive (list (gnus-summary-article-number)))
9324   (gnus-group-edit-global-kill article))
9325
9326 (defun gnus-summary-edit-local-kill ()
9327   "Edit a local kill file applied to the current newsgroup."
9328   (interactive)
9329   (setq gnus-current-headers (gnus-summary-article-header))
9330   (gnus-group-edit-local-kill
9331    (gnus-summary-article-number) gnus-newsgroup-name))
9332
9333 ;;; Header reading.
9334
9335 (defun gnus-read-header (id &optional header)
9336   "Read the headers of article ID and enter them into the Gnus system."
9337   (let ((group gnus-newsgroup-name)
9338         (gnus-override-method
9339          (or
9340           gnus-override-method
9341           (and (gnus-news-group-p gnus-newsgroup-name)
9342                (car (gnus-refer-article-methods)))))
9343         where)
9344     ;; First we check to see whether the header in question is already
9345     ;; fetched.
9346     (if (stringp id)
9347         ;; This is a Message-ID.
9348         (setq header (or header (gnus-id-to-header id)))
9349       ;; This is an article number.
9350       (setq header (or header (gnus-summary-article-header id))))
9351     (if (and header
9352              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9353         ;; We have found the header.
9354         header
9355       ;; If this is a sparse article, we have to nix out its
9356       ;; previous entry in the thread hashtb.
9357       (when (and header
9358                  (gnus-summary-article-sparse-p (mail-header-number header)))
9359         (let* ((parent (gnus-parent-id (mail-header-references header)))
9360                (thread (and parent (gnus-id-to-thread parent))))
9361           (when thread
9362             (delq (assq header thread) thread))))
9363       ;; We have to really fetch the header to this article.
9364       (save-excursion
9365         (set-buffer nntp-server-buffer)
9366         (when (setq where (gnus-request-head id group))
9367           (nnheader-fold-continuation-lines)
9368           (goto-char (point-max))
9369           (insert ".\n")
9370           (goto-char (point-min))
9371           (insert "211 ")
9372           (princ (cond
9373                   ((numberp id) id)
9374                   ((cdr where) (cdr where))
9375                   (header (mail-header-number header))
9376                   (t gnus-reffed-article-number))
9377                  (current-buffer))
9378           (insert " Article retrieved.\n"))
9379         (if (or (not where)
9380                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9381             ()                          ; Malformed head.
9382           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9383             (when (and (stringp id)
9384                        (not (string= (gnus-group-real-name group)
9385                                      (car where))))
9386               ;; If we fetched by Message-ID and the article came
9387               ;; from a different group, we fudge some bogus article
9388               ;; numbers for this article.
9389               (mail-header-set-number header gnus-reffed-article-number))
9390             (save-excursion
9391               (set-buffer gnus-summary-buffer)
9392               (decf gnus-reffed-article-number)
9393               (gnus-remove-header (mail-header-number header))
9394               (push header gnus-newsgroup-headers)
9395               (setq gnus-current-headers header)
9396               (push (mail-header-number header) gnus-newsgroup-limit)))
9397           header)))))
9398
9399 (defun gnus-remove-header (number)
9400   "Remove header NUMBER from `gnus-newsgroup-headers'."
9401   (if (and gnus-newsgroup-headers
9402            (= number (mail-header-number (car gnus-newsgroup-headers))))
9403       (pop gnus-newsgroup-headers)
9404     (let ((headers gnus-newsgroup-headers))
9405       (while (and (cdr headers)
9406                   (not (= number (mail-header-number (cadr headers)))))
9407         (pop headers))
9408       (when (cdr headers)
9409         (setcdr headers (cddr headers))))))
9410
9411 ;;;
9412 ;;; summary highlights
9413 ;;;
9414
9415 (defun gnus-highlight-selected-summary ()
9416   "Highlight selected article in summary buffer."
9417   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9418   (when gnus-summary-selected-face
9419     (save-excursion
9420       (let* ((beg (progn (beginning-of-line) (point)))
9421              (end (progn (end-of-line) (point)))
9422              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9423              (from (if (get-text-property beg gnus-mouse-face-prop)
9424                        beg
9425                      (or (next-single-property-change
9426                           beg gnus-mouse-face-prop nil end)
9427                          beg)))
9428              (to
9429               (if (= from end)
9430                   (- from 2)
9431                 (or (next-single-property-change
9432                      from gnus-mouse-face-prop nil end)
9433                     end))))
9434         ;; If no mouse-face prop on line we will have to = from = end,
9435         ;; so we highlight the entire line instead.
9436         (when (= (+ to 2) from)
9437           (setq from beg)
9438           (setq to end))
9439         (if gnus-newsgroup-selected-overlay
9440             ;; Move old overlay.
9441             (gnus-move-overlay
9442              gnus-newsgroup-selected-overlay from to (current-buffer))
9443           ;; Create new overlay.
9444           (gnus-overlay-put
9445            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9446            'face gnus-summary-selected-face))))))
9447
9448 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9449 (defun gnus-summary-highlight-line ()
9450   "Highlight current line according to `gnus-summary-highlight'."
9451   (let* ((list gnus-summary-highlight)
9452          (p (point))
9453          (end (progn (end-of-line) (point)))
9454          ;; now find out where the line starts and leave point there.
9455          (beg (progn (beginning-of-line) (point)))
9456          (article (gnus-summary-article-number))
9457          (score (or (cdr (assq (or article gnus-current-article)
9458                                gnus-newsgroup-scored))
9459                     gnus-summary-default-score 0))
9460          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9461          (inhibit-read-only t))
9462     ;; Eval the cars of the lists until we find a match.
9463     (let ((default gnus-summary-default-score))
9464       (while (and list
9465                   (not (eval (caar list))))
9466         (setq list (cdr list))))
9467     (let ((face (cdar list)))
9468       (unless (eq face (get-text-property beg 'face))
9469         (gnus-put-text-property-excluding-characters-with-faces
9470          beg end 'face
9471          (setq face (if (boundp face) (symbol-value face) face)))
9472         (when gnus-summary-highlight-line-function
9473           (funcall gnus-summary-highlight-line-function article face))))
9474     (goto-char p)))
9475
9476 (defun gnus-update-read-articles (group unread &optional compute)
9477   "Update the list of read articles in GROUP."
9478   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9479          (entry (gnus-gethash group gnus-newsrc-hashtb))
9480          (info (nth 2 entry))
9481          (prev 1)
9482          (unread (sort (copy-sequence unread) '<))
9483          read)
9484     (if (or (not info) (not active))
9485         ;; There is no info on this group if it was, in fact,
9486         ;; killed.  Gnus stores no information on killed groups, so
9487         ;; there's nothing to be done.
9488         ;; One could store the information somewhere temporarily,
9489         ;; perhaps...  Hmmm...
9490         ()
9491       ;; Remove any negative articles numbers.
9492       (while (and unread (< (car unread) 0))
9493         (setq unread (cdr unread)))
9494       ;; Remove any expired article numbers
9495       (while (and unread (< (car unread) (car active)))
9496         (setq unread (cdr unread)))
9497       ;; Compute the ranges of read articles by looking at the list of
9498       ;; unread articles.
9499       (while unread
9500         (when (/= (car unread) prev)
9501           (push (if (= prev (1- (car unread))) prev
9502                   (cons prev (1- (car unread))))
9503                 read))
9504         (setq prev (1+ (car unread)))
9505         (setq unread (cdr unread)))
9506       (when (<= prev (cdr active))
9507         (push (cons prev (cdr active)) read))
9508       (setq read (if (> (length read) 1) (nreverse read) read))
9509       (if compute
9510           read
9511         (save-excursion
9512           (let (setmarkundo)
9513             ;; Propagate the read marks to the backend.
9514             (when (gnus-check-backend-function 'request-set-mark group)
9515               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9516                     (add (gnus-remove-from-range read (gnus-info-read info))))
9517                 (when (or add del)
9518                   (unless (gnus-check-group group)
9519                     (error "Can't open server for %s" group))
9520                   (gnus-request-set-mark
9521                    group (delq nil (list (if add (list add 'add '(read)))
9522                                          (if del (list del 'del '(read))))))
9523                   (setq setmarkundo
9524                         `(gnus-request-set-mark
9525                           ,group
9526                           ',(delq nil (list
9527                                        (if del (list del 'add '(read)))
9528                                        (if add (list add 'del '(read))))))))))
9529             (set-buffer gnus-group-buffer)
9530             (gnus-undo-register
9531               `(progn
9532                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9533                  (gnus-info-set-read ',info ',(gnus-info-read info))
9534                  (gnus-get-unread-articles-in-group ',info 
9535                                                     (gnus-active ,group))
9536                  (gnus-group-update-group ,group t)
9537                  ,setmarkundo))))
9538         ;; Enter this list into the group info.
9539         (gnus-info-set-read info read)
9540         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9541         (gnus-get-unread-articles-in-group info (gnus-active group))
9542         t))))
9543
9544 (defun gnus-offer-save-summaries ()
9545   "Offer to save all active summary buffers."
9546   (save-excursion
9547     (let ((buflist (buffer-list))
9548           buffers bufname)
9549       ;; Go through all buffers and find all summaries.
9550       (while buflist
9551         (and (setq bufname (buffer-name (car buflist)))
9552              (string-match "Summary" bufname)
9553              (save-excursion
9554                (set-buffer bufname)
9555                ;; We check that this is, indeed, a summary buffer.
9556                (and (eq major-mode 'gnus-summary-mode)
9557                     ;; Also make sure this isn't bogus.
9558                     gnus-newsgroup-prepared
9559                     ;; Also make sure that this isn't a dead summary buffer.
9560                     (not gnus-dead-summary-mode)))
9561              (push bufname buffers))
9562         (setq buflist (cdr buflist)))
9563       ;; Go through all these summary buffers and offer to save them.
9564       (when buffers
9565         (map-y-or-n-p
9566          "Update summary buffer %s? "
9567          (lambda (buf)
9568            (switch-to-buffer buf)
9569            (gnus-summary-exit))
9570          buffers)))))
9571
9572 (defun gnus-summary-setup-default-charset ()
9573   "Setup newsgroup default charset."
9574   (if (equal gnus-newsgroup-name "nndraft:drafts")
9575       (setq gnus-newsgroup-charset nil)
9576     (let* ((name (and gnus-newsgroup-name
9577                       (gnus-group-real-name gnus-newsgroup-name)))
9578            (ignored-charsets
9579             (or gnus-newsgroup-ephemeral-ignored-charsets
9580                 (append
9581                  (and gnus-newsgroup-name
9582                       (or (gnus-group-find-parameter gnus-newsgroup-name
9583                                                      'ignored-charsets t)
9584                           (let ((alist gnus-group-ignored-charsets-alist)
9585                                 elem (charsets nil))
9586                             (while (setq elem (pop alist))
9587                               (when (and name
9588                                          (string-match (car elem) name))
9589                                 (setq alist nil
9590                                       charsets (cdr elem))))
9591                             charsets)))
9592                  gnus-newsgroup-ignored-charsets))))
9593       (setq gnus-newsgroup-charset
9594             (or gnus-newsgroup-ephemeral-charset
9595                 (and gnus-newsgroup-name
9596                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9597                          (let ((alist gnus-group-charset-alist)
9598                                elem charset)
9599                            (while (setq elem (pop alist))
9600                              (when (and name
9601                                         (string-match (car elem) name))
9602                                (setq alist nil
9603                                      charset (cadr elem))))
9604                            charset)))
9605                 gnus-default-charset))
9606       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9607            ignored-charsets))))
9608
9609 ;;;
9610 ;;; Mime Commands
9611 ;;;
9612
9613 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9614   "Display the current article buffer fully MIME-buttonized.
9615 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9616 treated as multipart/mixed."
9617   (interactive "P")
9618   (require 'gnus-art)
9619   (let ((gnus-unbuttonized-mime-types nil)
9620         (gnus-mime-display-multipart-as-mixed show-all-parts))
9621     (gnus-summary-show-article)))
9622
9623 (defun gnus-summary-repair-multipart (article)
9624   "Add a Content-Type header to a multipart article without one."
9625   (interactive (list (gnus-summary-article-number)))
9626   (gnus-with-article article
9627     (message-narrow-to-head)
9628     (goto-char (point-max))
9629     (widen)
9630     (when (search-forward "\n--" nil t)
9631       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9632         (message-narrow-to-head)
9633         (message-remove-header "Mime-Version")
9634         (message-remove-header "Content-Type")
9635         (goto-char (point-max))
9636         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9637                         separator))
9638         (insert "Mime-Version: 1.0\n")
9639         (widen))))
9640   (let (gnus-mark-article-hook)
9641     (gnus-summary-select-article t t nil article)))
9642
9643 (defun gnus-summary-toggle-display-buttonized ()
9644   "Toggle the buttonizing of the article buffer."
9645   (interactive)
9646   (require 'gnus-art)
9647   (if (setq gnus-inhibit-mime-unbuttonizing
9648             (not gnus-inhibit-mime-unbuttonizing))
9649       (let ((gnus-unbuttonized-mime-types nil))
9650         (gnus-summary-show-article))
9651     (gnus-summary-show-article)))
9652
9653 ;;;
9654 ;;; Generic summary marking commands
9655 ;;;
9656
9657 (defvar gnus-summary-marking-alist
9658   '((read gnus-del-mark "d")
9659     (unread gnus-unread-mark "u")
9660     (ticked gnus-ticked-mark "!")
9661     (dormant gnus-dormant-mark "?")
9662     (expirable gnus-expirable-mark "e"))
9663   "An alist of names/marks/keystrokes.")
9664
9665 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9666 (defvar gnus-summary-mark-map)
9667
9668 (defun gnus-summary-make-all-marking-commands ()
9669   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9670   (dolist (elem gnus-summary-marking-alist)
9671     (apply 'gnus-summary-make-marking-command elem)))
9672
9673 (defun gnus-summary-make-marking-command (name mark keystroke)
9674   (let ((map (make-sparse-keymap)))
9675     (define-key gnus-summary-generic-mark-map keystroke map)
9676     (dolist (lway `((next "next" next nil "n")
9677                     (next-unread "next unread" next t "N")
9678                     (prev "previous" prev nil "p")
9679                     (prev-unread "previous unread" prev t "P")
9680                     (nomove "" nil nil ,keystroke)))
9681       (let ((func (gnus-summary-make-marking-command-1
9682                    mark (car lway) lway name)))
9683         (setq func (eval func))
9684         (define-key map (nth 4 lway) func)))))
9685       
9686 (defun gnus-summary-make-marking-command-1 (mark way lway name)
9687   `(defun ,(intern
9688             (format "gnus-summary-put-mark-as-%s%s"
9689                     name (if (eq way 'nomove)
9690                              ""
9691                            (concat "-" (symbol-name way)))))
9692      (n)
9693      ,(format
9694        "Mark the current article as %s%s.
9695 If N, the prefix, then repeat N times.
9696 If N is negative, move in reverse order.
9697 The difference between N and the actual number of articles marked is
9698 returned."
9699        name (cadr lway))
9700      (interactive "p")
9701      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9702     
9703 (defun gnus-summary-generic-mark (n mark move unread)
9704   "Mark N articles with MARK."
9705   (unless (eq major-mode 'gnus-summary-mode)
9706     (error "This command can only be used in the summary buffer"))
9707   (gnus-summary-show-thread)
9708   (let ((nummove
9709          (cond
9710           ((eq move 'next) 1)
9711           ((eq move 'prev) -1)
9712           (t 0))))
9713     (if (zerop nummove)
9714         (setq n 1)
9715       (when (< n 0)
9716         (setq n (abs n)
9717               nummove (* -1 nummove))))
9718     (while (and (> n 0)
9719                 (gnus-summary-mark-article nil mark)
9720                 (zerop (gnus-summary-next-subject nummove unread t)))
9721       (setq n (1- n)))
9722     (when (/= 0 n)
9723       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9724     (gnus-summary-recenter)
9725     (gnus-summary-position-point)
9726     (gnus-set-mode-line 'summary)
9727     n))
9728
9729 (gnus-summary-make-all-marking-commands)
9730
9731 (gnus-ems-redefine)
9732
9733 (provide 'gnus-sum)
9734
9735 (run-hooks 'gnus-sum-load-hook)
9736
9737 ;;; gnus-sum.el ends here